Jump to content

Can't pass DOM object to function without breaking everything


Recommended Posts

Javascript is incredibly frustrating.

 

I'm trying to display a paragraph beneath a select field based on what the user selects. I have these two functions:

 

function toggleDisplay(myId) {
  if (document.getElementById(myId).style.display == 'none') {
    document.getElementById(myId).style.display = 'block';
  } else {
    document.getElementById(myId).style.display = 'none';
  }
}

function changeValue(fieldId1, fieldId2) {
  document.getElementById(fieldId1).value = document.getElementById(fieldId2).value;
}

 

Both of these functions work fine independently. Now, here is my select field with a text field above it to store the previous value of the select field:

 

<input type="text" id="prevValue" value="" />
<select id="thisValue" onChange="toggleDisplay(document.getElementById(prevValue).value);changeValue('prevValue', 'thisValue');toggleDisplay(this.value);">

 

Here is the way I want it to work once a user selects a value from the select field:

[*]The element with the corresponding id in the text field is closed

[*]The value in the text field is changed to the value selected by the user from the select field

[*]The element with the corresponding id selected by the user from the select field is opened

 

Of course, it does absolutely NOTHING unless I take out toggleDisplay(document.getElementById(prevValue).value); from the select field. This makes it:

 



<input type="text" id="prevValue" value="" />
<select id="thisValue" onChange="changeValue('prevValue', 'thisValue');toggleDisplay(this.value);">

 

As soon as I take that part out, both functions work again. Why can't I pass the object document.getElementById(prevValue).value to the toggleDisplay function without breaking everything?!

 

Any help at all is appreciated.

I thought for sure I found the problem, but I didn't. I forgot to put quotes around 'prevValue' in my object reference:

 

<input type="text" id="prevValue" value="" />
<select id="thisValue" onChange="toggleDisplay(document.getElementById('prevValue').value);changeValue('prevValue', 'thisValue');toggleDisplay(this.value);">

 

Still doesn't work.

I feel like such an idiot when I answer my own questions.

 

I had to set the value of the text field to something by default, and make a dummy element with that id in the document.

 

It would be nice if JavaScript threw errors of some sort... like "toggleDisplay() failed: No object matches that id." or something.

I feel like such an idiot when I answer my own questions.

 

I had to set the value of the text field to something by default, and make a dummy element with that id in the document.

 

It would be nice if JavaScript threw errors of some sort... like "toggleDisplay() failed: No object matches that id." or something.

 

Use Firebug while you develop.  It has nice JavaScript errors.

  • 4 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.