45

If I have this code:

 <select onchange="alert('?');" name="myname" class="myclass"> 
    <option isred="-1" value="hi">click</option>
 </select>

How can I get the value '-1' from the custom attribute isred ? I don't want to use the value property. And I dont want to target the option tag by a name or id.

I want something like onchange="alert(this.getselectedoptionID.getAttribute('isred'));"

Can anyone help?

Also I don't want to use jquery.

Steve Robbins
  • 13,672
  • 12
  • 76
  • 124
omega
  • 40,311
  • 81
  • 251
  • 474
  • 1
    possible duplicate of [How do you get the currently selected – Felix Kling Aug 14 '12 at 17:37

7 Answers7

84

You need to figure out what the selectedIndex is, then getAttribute from that options[] Array.

<select onchange="alert(this.options[this.selectedIndex].getAttribute('isred'));" name="myname" class="myclass"> 
    <option isred="-1" value="hi">click</option>
    <option isred="-5" value="hi">click</option>
</select>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

jsFiddle DEMO

As a side note:

Don't use inline javascript in your HTML. You want to separate your business logic from your UI. Create a javascript event handlers instead to handle this. (jQuery / Angular / etc)

Mark Pieszak - Trilon.io
  • 61,391
  • 14
  • 82
  • 96
  • @Mark Pieszak, Imagine I have this code: `` What better way do you suggest instead of inline onchange declaration? I want values to be passed in as parameters! Thanks in Advance! – sbsatter May 27 '17 at 06:46
  • @ShahedBinSatter Set up the change event in the javascript – Mark Pieszak - Trilon.io May 29 '17 at 16:06
  • How do I pass the values (this.value and this.getAttribute('code')) as parameters? – sbsatter May 30 '17 at 03:59
  • `onchange="myFunc(this.value, this.getAttribute('code'))"` with "myFunc" being a `function myFunc(val, code) { /* do something */ }` in your actual JS code. Ideally don't even use the `onchange` method, and setup that onchange method in the JS itself, but that's a separate concept, but you can look that up online and see how it's done! Hope that helps. Cheers – Mark Pieszak - Trilon.io May 30 '17 at 15:02
32

in jquery, you can just write:

$("#myname").find(':selected').attr('isred');
bensiu
  • 24,660
  • 56
  • 77
  • 117
Bernard
  • 465
  • 4
  • 3
8

Use something like this:

document.getElementById("x").onchange = function () {
    console.log(this.options[this.selectedIndex].getAttribute("isred"));
};
Anthony Mills
  • 8,676
  • 4
  • 32
  • 51
5

//Pure Javascript solution, and elegant one check if you really want to leverage the power of javascript.

// Listening to a onchange event by ID attached with the select tag.
document.getElementById("name_your_id").onchange = function(event) {

//event.target.selectedOptions[0] have that option. as this is single selection by dropdown. this will always be 0th index :) 
let get_val = event.target.selectedOptions[0].getAttribute("isred");
console.log("Value from the Attribute: ", get_val)
}
 <select id="name_your_id" name="myname" class="myclass">
    <option isred="423423" value="hi">One</option>
    <option isred="-1" value="hi">Two</option>
 </select>
Code Cooker
  • 881
  • 14
  • 19
3

Assuming we have a HTML markup as below:

<form id="frm_">
    <select name="Veh">
        <option value='-1' selected='selected'>Select</option>
        <option value='0' ren='x'>xxx</option>
        <option value='1' ren='y'>yyy</option>
    </select>
</form>

The attr "ren" can be accessed like this:

function getRep() {
    var ren = document.forms['frm_'].elements['Veh'].options[document.forms['frm_']
                 .elements['Veh'].selectedIndex].getAttribute('ren');
    console.log("Val of ren " + ren); //x or y
}

Demo

Shubh
  • 6,693
  • 9
  • 48
  • 83
walter
  • 41
  • 1
  • 1
    the code provided by you had errors which needs to be addressed before you post it for others to use. Your answer would be more helpful if you post your important piece of code with the answer rather than user's navigating to your jsfiddle link. As of now I have done it on behalf of you. – Shubh Jul 02 '15 at 04:12
1

You use: .getAttribute('isred')

You want:

<select onchange="alert(this.options[this.selectedIndex].getAttribute('isred'));" name="myname" class="myclass">
    <option isred="-1" value="hi">click</option>
    <option isred="-1" value="ho">click</option>
</select>
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
-1

you can

$(".myclass").val(function(){alert($("option",this).attr("isred"));})
Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
  • 1
    This always alerts the value of the first option's (as opposed to the selected one) isred attribute, it deselects the selected option, and the question says **not** to use jQuery. – Quentin Oct 05 '13 at 06:33