3

I am trying to get an attribute from the child of the the currentTarget. I have tried this, (e.currentTarget.child.attr("data-english")

I have googled it and have had no luck finding anything useful, maybe its me being new with code I might be looking up the wrong thing.

Here is the HTML

 < select class="form-control" id="select_fundFamily"     name="select_fundFamily" autocomplete="off" >
  < option value="stuff" data-english="english" data-family-locallang="model" >

At the moment in JQuery, the e.currentTarget.value; is showing the select element.

Can someone please kindly advise me in what path to take to solve this issue?

Daedalus
  • 7,586
  • 5
  • 36
  • 61
user3457760
  • 311
  • 4
  • 11

3 Answers3

6

For the value of the selected option in a select, you can use

$(e.currentTarget).find(':selected').attr("data-english");

Run the snippet below to see it work

$(document).ready(function(){
    $('select').change(function(e){
         var eng = $(e.currentTarget).find(':selected').attr("data-english");
        $('body').append(eng);
    })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="select_fundFamily"> 
    <option selected disabled>Select something</option>
    <option value="stuff1" data-english="english1" >1</option>
    <option value="stuff2" data-english="english2" >2</option>
    <option value="stuff3" data-english="english3" >3</option>
    <option value="stuff4" data-english="english4" >4</option>
    <option value="stuff5" data-english="english5" >5</option>
</select>
Ted
  • 14,757
  • 2
  • 41
  • 58
2

If you try to determine the selected option's attribute. A simple solution is:

var dataEnglish = $('#select_fundFamily option:selected').attr('data-english'); // dataEnglish = english
adelindev
  • 538
  • 4
  • 10
  • Perfect! Thanks for your help! – user3457760 Jun 10 '15 at 20:09
  • 1
    A more elegant way - in case you want to determine that when the 'change' event occurs - you can also have it done like: $('#select_fundFamily').on('change', function (e) { var option = $("option:selected", this); console.log(option.attr('data-english')); // english }); – adelindev Jun 10 '15 at 20:11
1

currentTarget will respond any element that event listener triggered the event. In your case, i think you want to use e.target to target the right element.

$("select#select_fundFamily").on('click','option', function(e) {
    e.preventDefault();
    $(e.target).attr('data-english'); // you can use $(this) instead
    //something else
});
Hien Luong
  • 510
  • 1
  • 4
  • 15