You are viewing limited content. For full access, please sign in.

Question

Question

Retrieve CSS Class Name within Javascript code

asked on February 25, 2016

Hello,

 

I have Javacript code (excerpt):

$('.lookupCondition1 input, .lookupCondition2 input').change(function () {

  //...code comes here

});

Within the code I need to retrieve the string corresponding to which one of class names 'lookupCondition1' or 'lookupcondition2' has initiated the execution.

 

Thanks in advance !

0 0

Replies

replied on February 25, 2016

Hi Stephane,

Retrieving a selector's string (the easy way) has been deprecated as of Jquery 1.7

However, if you want to select the Jquery element that triggered your function, you can use $(this) 

For instance, $(this).val('test') will insert 'test' into whichever input triggered your function.

Let me know if this works for you!

0 0
replied on February 25, 2016

Hello Alexander,

Let me try to rephrase my question: is there any mechanism within the above function to retrieve which one of {lookupCondition1,lookupCondition2} CSS classes did serve to run the function instance?

Regards

0 0
replied on February 25, 2016

Hi Stephane,

Like I said, the mechanism phased out in Jquery 1.7. However, you can use JavaScript "if" statements as a workaround. For instance:

$('.lookupCondition1 input, .lookupCondition2 input').on('change', function(){
  var initiator = '';
  
  if ($(this).parents().hasClass('lookupCondition1')){
    initiator = 'lookupCondition1';
  }
  else if($(this).parents().hasClass('lookupCondition2')){
    initiator = 'lookupCondition2';
  }
});

Whichever class triggers the function, the "initiator" variable will take its value.

0 0
replied on February 26, 2016

Thank's Alexander for your help !

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.