Tinus Jackson Tinus Jackson - 3 months ago 7
jQuery Question

Create a help click function jquery / javascript

I want to create a help function on a site.

I have a button (help). Once the user clicks on that button, a listener listens to where the user clicks next on the page and I want that elements info to be extracted.

I can use this information then to search on another site.

New to jquery and javascript have this so far , not sure how to continue or change

$("#help" ).on( "click", function() {
alert( $( this ).text() );
});
$( "#help").trigger( "click" );


Please help me with this.

Answer

You can use this approach if you have fewer elements for which you want this functionality.

You can keep a local variable say blIsHelpActivated that gets true / false on click of your help button. When help button is activated you can set that variable to true, when its inactivated then you can set it to false.

Now you can attach on click event of controls on your page. In that you will need to check that variable and do the searching part if its value is true.

var blIsHelpActivated = false;

$("#help").on("click", function() {
    // Toggle boolean to identify whether help is activated or not.
    if(blIsHelpActivated)
    {
      blIsHelpActivated = false;
    }
    else
    {
      blIsHelpActivated = true;
    }
 });

 // Code to attach click event on elements.
 $("#yourelementid").on("click", function() {
    if(blIsHelpActivated)
    {
      // Your serarching code goes here.
    }
});

Altervatively, you can apply a common single class to each elements and you can use below code, instead of attaching click event to each elements.

 // Code to handle click event on elements.
 $(".yourcommonclassname").on("click", function() {
    if(blIsHelpActivated)
    {
      // Fetch current clicked element, by using $(this)
      // And further search based on your logic.
    }
});
Comments