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.
}
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.
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.
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.
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>
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.
This provides good reference on how to write a function to capture the change - http://api.jquery.com/change/
$('#ddl').change(function() {
// do something
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With