Javascript performance using the event properties vs using the element id

Viewed 25

In Javascript/Jquery what is faster/better. Using the event properties as such:

$('#aPQD_Employer').on('change', function(e){
    (e.currentTarget.selectedOptions[0].label == "Other")?
        $('#aPQD_EmployerOtherDIV').show()
        :
        $('#aPQD_EmployerOtherDIV').hide();
});

VS

$('#aPQD_Employer').on('change', function(e){
    ($('#aPQD_Employer').find(":selected").text() == "Other")?
        $('#aPQD_EmployerOtherDIV').show()
        :
        $('#aPQD_EmployerOtherDIV').hide();    
});
1 Answers

Please show the HTML - what is an option.label?

It is not recommended to use a side effect of a ternary in my opinion

Also use .toggle():

$('#aPQD_Employer').on('change', function(){
  $('#aPQD_EmployerOtherDIV').toggle(this.value=="Other") 
});

If you must use the text, you can use

.toggle($("option:selected", this).text()=="Other");
Related