Jump to content

[SOLVED] Getting the option value of a select field


gerkintrigg

Recommended Posts

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.