Why do I need to give my options a value attribute in my dropdown? JQuery related.

Posted by Alex on Stack Overflow See other posts from Stack Overflow or by Alex
Published on 2010-05-10T06:21:13Z Indexed on 2010/05/10 6:34 UTC
Read the original article Hit count: 181

Filed under:
|

So far in my web developing experiences, I've noticed that almost all web developers/designers choose to give their options in a select a value like so:

<select name="foo">
    <option value="bar">BarCheese</option>
    // etc.
    // etc.
</select>

Is this because it is best practice to do so? I ask this because I have done a lot of work with jQuery and dropdown's lately, and sometimes I get really annoyed when I have to check something like:

$('select[name=foo]').val() == "bar"); 

To me, many times that seems less clear than just being able to check the val() against BarCheese. So why is it that most web developers/designers specify a value paramater instead of just letting the options actual value be its value?

And yes, if the option has a value attribute I know I can do something like this:

$('select[name=foo] option:contains("BarCheese")').attr('selected', 'selected');

But I would still really like to know why this is done.

Thanks!!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about best-practices