mjsulliv Posted April 28, 2010 Share Posted April 28, 2010 var protocolVar = document.enterDataForm_NAME.protocol_sel_NAME.value; The above assignment works in firefox but not IE. Is there a syntax that will work with both?enterDataForm_NAME is the NAME of the form element and protocol_sel_NAME is the NAME of a select element.Thanks --- Mike Link to comment Share on other sites More sharing options...
ShadowMage Posted April 28, 2010 Share Posted April 28, 2010 Dot notation is old school. You should give your elements id attributes and use getElementById(). That will work on all browsers.So you have a select element, you give it an id: <select name='protocol_sel_NAME' id='protocol_sel_ID'>...</select> If it is in a form that will be submitted (which I assume it is since you mention having a form element) it needs to keep the name attribute. The id and the name can be the same or different, it doesn't matter.Then you get a reference to that element like this: var protocolVar = document.getElementById("protocol_sel_ID"); or if you want to set protocolVar equal to the value of the select do it like this: var protocolVar = document.getElementById("protocol_sel_ID").value; Edit: Note that the value property of a select can be unreliable. The better way to get the selected value of a select is like so: var mySelect = document.getElementById("protocol_sel_ID");var protocolVar = mySelect.options[mySelect.selectedIndex].value; BTW, the name attribute of form tags is deprecated and should not be used if possible. If you need a reference to the form give it an id and use the same technique as above. Link to comment Share on other sites More sharing options...
mjsulliv Posted April 29, 2010 Author Share Posted April 29, 2010 jkloth, thank you. A very complete explanation.--- Mike Link to comment Share on other sites More sharing options...
mjsulliv Posted April 29, 2010 Author Share Posted April 29, 2010 Hi. IE is still not playing nice. I've changed the code to: var protocolSelect = document.getElementById("protocol_sel_ID");var protocolVal = protocolSelect.options[protocolSelect.selectedIndex].value; and IE still has a empty string for the value -- Firefox is fine. Using the IE debugger I can see that it has found the correct select element, that selectedIndex is correct, even the innerHTML has the list of option values; but ... var protocolVal = protocolSelect.options[protocolSelect.selectedIndex].value; always returns an empty string.Any thoughts?thanks --- Mike Link to comment Share on other sites More sharing options...
ShadowMage Posted April 29, 2010 Share Posted April 29, 2010 What does your HTML for the select look like? Make sure that your options have a value:...<option value='something'>Option</option>... Link to comment Share on other sites More sharing options...
mjsulliv Posted April 30, 2010 Author Share Posted April 30, 2010 What does your HTML for the select look like? Make sure that your options have a value:jkloth, that looks like it was the issue. I build the option elements dynamically and was only initializing the innerHTML attribute. Do I need the innerHTML or is there a "more standard" syntax for setting the text in the element?Thanks again for the help. --- Mike Link to comment Share on other sites More sharing options...
ShadowMage Posted April 30, 2010 Share Posted April 30, 2010 Typically you would work with the value of the select. The only time you need to worry about innerHTML is if you want to change the text that is displayed to the user. Firefox apparently sets the value equal to whatever is in innerHTML if a value is not defined, IE does not. I'm not sure about other browsers but it's a good idea to always define a value when you create your <option>'s.If your form is being submitted to a server-side script you will need to have the value set since that's what gets submitted. Link to comment Share on other sites More sharing options...
mjsulliv Posted April 30, 2010 Author Share Posted April 30, 2010 Cool -- Thanks again! Link to comment Share on other sites More sharing options...
Firasmith Posted May 4, 2010 Share Posted May 4, 2010 Typically you would work with the value of the select. The only time you need to worry about innerHTML is if you want to change the text that is displayed to the user. Firefox apparently sets the value equal to whatever is in innerHTML if a value is not defined, IE does not. I'm not sure about other browsers but it's a good idea to always define a value when you create your <option>'s.If your form is being submitted to a server-side script you will need to have the value set since that's what gets submitted.If you wanted to not even worry about even that after the pade is set, couldn't you link to a Xml file and use formating in the css and the I forget the thigns that you can use to format the xml data sheets when you display them in the html-- or a simple Ping image? >.< Link to comment Share on other sites More sharing options...
ShadowMage Posted May 4, 2010 Share Posted May 4, 2010 If you wanted to not even worry about even that after the pade is set, couldn't you link to a Xml file and use formating in the css and the I forget the thigns that you can use to format the xml data sheets when you display them in the html-- or a simple Ping image? >.<I think that's more work than it's worth for what the OP was trying to accomplish. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.