views:

76

answers:

6

I want to carry some extra hidden information (eg: postal code) in an HTML select/option dropdown list and make it available to a javascript function when user changes option box selection.

This is the type of thing I would like to do (but alas it does not work). Can someone fix it or suggest reasonable alternative?

    <select id="sel_activity" onchange="selectionChange(this.info)"> 
    <option info=""       value="CAR">CAR PROBLEM</option> 
    <option info=""       value="COFFEE">Coffee Break</option>
    <option info="45678"  value="INV">INVENTORY COUNT</option>
    <option info="23567"  value="INVDROP">Inventory</option>
    <option info="" value="LUNCH">Lunch Break</option> 
    <option info="87654"  value="MEET">Meeting</option>
    </select>

.
.
.
.
    function selectionChange(info){

       alert(info);

    }
+4  A: 

HTML 5 provides data-* attributes, you can define your own attributes just prefix them with data-. Like data-info, data-zip or whatever.

Maxem
Thanks, but maybe in a year or two...don't ask me what browsers my users are using.. :(
robert
Not really meant for that, but you could use the class attribute, should be supported on option tags.
Maxem
@robert: Since you're already using a non-standard attribute "`info`", you may as well make it HTML5-compliant. JS on this will still work, and it's a better option than abusing the class attribute.
You
I believe custom attributes wont "break" any browser as such - they will just cause invalid HTML in browsers where HTML 5 is not supported, or if you set the doctype to anything below HTML 5. What I mean is that, no matter what browsers your users are using, you are ok to use custom attributes... they may just not be valid in some browsers.
ClarkeyBoy
A: 

onchange="selectionChange(this.options.item(this.selectedIndex).getAttribute('info'))"

idealmachine
A: 

The easiest will be to have a parallel array and given the value of the selection, it will find the value in the array to reveal additional info value that you want.

Another option is to call a web service to look up the info given the value if it is not an overkill for your type of project

Fadrian Sudaman
A: 

class and id are good attributes to use for this kind of information storage. One thing to keep in mind though is that, by W3C standards, id cannot start with a number, so if you were hoping to store numbers as your extra info you'll either have to use class or implement some extra Javascript that pulls the number from the id, like a substr function.

Mathias Schnell
-1. `class` and `id` is not for information storage.
You
Usually, no, but it's a good trick to use until HTML5 becomes mainstream
Mathias Schnell
+2  A: 

I would use JQuery to get the attribute's value, using this function:

attr( attributeName )

something like this:

       $("select").change(function () {

          $("select option:selected").each(function () {
                var info = $(this).attr("info");
                alert(info);
              });

        })
        .trigger('change');

and you can set the attribute value with same function:

 attr( attributeName, value )

check the API

mklfarha
A: 

Since your users are using old browsers, a pipe or character delimited string may work.

Such as ...

<select id="sel_activity" onchange="selectionChange(this)">
  <option value="|CAR">CAR PROBLEM</option> 
  <option value="|COFFEE">Coffee Break</option>
  <option value="45678|INV">INVENTORY COUNT</option>
  <option value="23567|INVDROP">Inventory</option>
  <option value="|LUNCH">Lunch Break</option> 
  <option value="87654|MEET">Meeting</option>
</select>

With standard JavaScript ...

 function selectionChange(o) {
   // an alternative ...    
   var values = o.options[o.options.selectedIndex].value.split("|");            
   alert("Selected Index? "+(o.options.selectedIndex+1)
    +"\nInfo? "+values[0]
    +"\nValue? "+values[1]);
   values = null;
  }
seasonedgeek