Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if element has been clicked or changed

Tags:

jquery

I have some what of a stupid question and I think I already know the answer but I would Like to find out from someone with more jquery knowledge than I have. I have a drop down list and I would like to know if I can check to see if the ddl has been clicked or changed. Example

If($('#ddl').click() || $('#ddl').on('change'){
    //do something.
}
like image 456
Raymond Feliciano Avatar asked Aug 27 '13 17:08

Raymond Feliciano


People also ask

How can you tell if an element has been clicked?

To check if an element was clicked, add a click event listener to the element, e.g. button. addEventListener('click', function handleClick() {}) . The click event is dispatched every time the element is clicked.

How do you check if an element has been clicked jQuery?

jQuery click() Method The click event occurs when an element is clicked. The click() method triggers the click event, or attaches a function to run when a click event occurs.

Is clicked in jQuery?

To trigger the onclick function in jQuery, click() method is used. For example, on clicking a paragraph on a document, a click event will be triggered by the $(“p”). click() method. The user can attach a function to a click method whenever an event of a click occurs to run the function.

How do you check if submit button is clicked in jQuery?

To find out if a button is clicked previously or not you can use an hidden input html control and save the number of times your button is click in its value. The HTML of my page would have a button and an input control: <button id="myButton">Click Here</button>


2 Answers

You can bind a click and change event handler to the element and set a flag:

$('#ddl').on('click change', function() {
    $(this).data('clicked', true);
});

// later
if ($('#ddl').data('clicked')) {
   // ...
}

Of course if you want to perform an action when the element is clicked or changed, put that code directly in the event handler. You don't need a flag in that case.

like image 74
Felix Kling Avatar answered Oct 25 '22 15:10

Felix Kling


This provides good reference on how to write a function to capture the change - http://api.jquery.com/change/

$('#ddl').change(function() {
    // do something
});
like image 35
Jon La Marr Avatar answered Oct 25 '22 14:10

Jon La Marr