May 27, 2013

How to determine whether it was the class or id selector clicked in joint event handler?

Sephiith’s Question:

In the below event handler I need to determine whether it was ID or Class that was clicked and assign a variable based on that.

What would be the easiest way of determining that in an IF statement?

jQuery CODE:

   $(document).on('click', '.inter [class], .inter [id]', function () {
   prevClass = className;
   IF CLASS >>>>>>>   className = this.className.substring(1);
   IF ID >>>>>>>>>>   className = this.id.substring(1);


    var back = '<div id="_'+ prevClass +'"></div>';

    link[prevClass] = original;
    original = link[className];
    link[className] += back;


       $('.inter').fadeTo(250, 0.25, function () {
           $('.inter').html(link[className]); 

           $('.inter').css({'background-image': 'url("' + className + '.png")'});
           $('.inter').fadeTo(250, 1.00);

       });
   });
 });

Something like this will work: (this.id || this.className).substr(1)

This works

IF this.className.substring(1) !== "" className = this.className.substring(1);
IF this.id.substring(1) !== ""   className = this.id.substring(1);

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!