How to get child attribute from e.currentTarget.value JQuery

I am trying to get an attribute from a child currentTarget

. I tried this, ( e.currentTarget.child.attr("data-english"

)

I have googled and have no luck finding anything useful, maybe its I was new with code, I might be looking for 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" >

      

Currently in JQuery e.currentTarget.value;

, a select element is displayed on the element.

Can someone ask you to advise me in what way to take this problem?

+3


source to share


3 answers


For the value of the selected parameter in the element, you can use

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

      

Run the snapshot 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>
      

Run codeHide result


+3


source


If you try to determine the attribute of the selected option. A simple solution:



var dataEnglish = $('#select_fundFamily option:selected').attr('data-english'); // dataEnglish = english

      

+2


source


currentTarget will respond to any element that triggers the event. In your case, I think you want to use e.target to target the desired element.

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

      

+1


source







All Articles