1

Suppose I have this:

<select id="myselect">
<option rel="a">1</option>
<option rel="b">2</option>
<select>

How do I use JQuery so that onchange $("#myselect"), I alert the "rel" part of the option (not 1 or 2)

1
  • 1
    Bear in mind that rel is not an attribute of option, so this HTML won't validate and this may cause problems in some browsers (going in to Quirks mode). Commented Mar 17, 2010 at 22:37

4 Answers 4

3
$("#myselect").change(function() {
    alert($(this).find("option:selected").attr("rel"));
});
Sign up to request clarification or add additional context in comments.

Comments

2

How about this?

$("myselect").change(function(){
  $(this).children("option:selected").each(function () {
            alert($(this).attr('rel');
          });
});

Should work for multiple selects, too.

Comments

2
$("#myselect").change(function() {
    $("#myselect option:selected").each(function () {
        alert($(this).attr('rel'));
    });
});

Just to mention, my code will also work if you have the multiple="multiple" attribute on your SELECT element, alerting both values.

Comments

1

The 'rel' attribute is not supported on an option element, you should be using the 'value' attribute to specify a value for your options. I'm sure jQuery can fetch that much easier than an unsupported attribute.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.