gerkintrigg Posted September 8, 2008 Share Posted September 8, 2008 Hiya. I have the following code to get the value of a select field. <select name="menu1" onchange="edit_cat.hidden_category.value=this.value;"> <option value="56" id="boobs">unnamed1</option> <option value="5" id="fart">5</option> </select> The problem is that i want to get the option value (what is displayed) not the value (what is hidden). I've tried getting the label, but that doesn't (seem to) work. I hope this makes sense. please help. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 8, 2008 Share Posted September 8, 2008 I don't get it. ??? Quote Link to comment Share on other sites More sharing options...
obsidian Posted September 8, 2008 Share Posted September 8, 2008 In the context of a select field, you have to target the option's value instead: onchange="edit_cat.hidden_category.value=this.options[this.selectedIndex].value;" Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 8, 2008 Share Posted September 8, 2008 I don't think the OP wants the VALUE, I think he wants the TEXT. <select name="menu1" onchange="edit_cat.hidden_category.value=this.options[this.selectedIndex].text;"> Although, it dioesn't make any sense to me - why not just make the text and the value of the select field the same so youdon't need a hidden field. Using JS to populate a hidden field is about the worst use of JS that I can think of. Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted September 9, 2008 Author Share Posted September 9, 2008 it's for an elegant category editor script i'm writing... I need to be able to get the previous category text and the value at the same time. The hidden field will hold the category id for the database and the editor field will hold the NEW text while the select field holds both the old text and the current database key. I hope this makes sense... I think it's a very clean and easy way of editing a database category. - It makes sense to me anyway. <form id="edit_cat" name="edit_cat" method="post" action="add_cat.php"> Previous Category Name: <select name="menu1" onchange="edit_cat.category.value=this.options[this.selectedIndex].text; edit_cat.hidden_category.value=this.options[this.selectedIndex].value; "> <option value="1">Option 1</option> <option value="2">Option 2</option> </select> <br /> New Category Name: <input name="category" type="text" class="search" id="category" /> <br /> <input name="hidden_category" type="text" id="hidden_category" /> <input name="Submit" type="submit" class="search" value="Submit" style="position:relative; top:2px;"/> </form> please note : I made the hidden field a text field here, so that you can see what I'm doing. Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted September 9, 2008 Author Share Posted September 9, 2008 By the way - thanks a lot! Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 9, 2008 Share Posted September 9, 2008 I don't see the need for the hidden field. Can't you just use the SELECT field value for the category ID? Always try to implement your JavaScript such that the page will still work without it. It might be true that some implementations are for a closed network where the browser settings is a known variable, but it is good practice, nonetheless. In the implementation above, if the category text is not populated, that is OK since the user is wanting to edit the value anyway. But, relying upon JavaScript to populate a hidden field is very bad practice in my opinion. If the JavaSCript fails the page fails. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.