Is it possible to add a onclick
event to any button by jquery or something like we add class?
function onload()
{
//add a something() function to button by id
}
Just invoke the code you're trying to invoke: onclick="alert('hello')" If you want to define a function, do that separately in your JavaScript code and just invoke the function in onclick . (Or, even better, attach it as a handler from the JavaScript code so you're not writing in-line JavaScript in your HTML.)
Greetings! Yes, you can call two JS Function on one onClick. Use semicolon (';') between both the functions.
The onclick event executes a certain functionality when a button is clicked. This could be when a user submits a form, when you change certain content on the web page, and other things like that. You place the JavaScript function you want to execute inside the opening tag of the button.
The onclick event generally occurs when the user clicks on an element. It allows the programmer to execute a JavaScript's function when an element gets clicked. This event can be used for validating a form, warning messages and many more. Using JavaScript, this event can be dynamically added to any element.
Calling your function something
binding the click
event on the element with a ID
$('#id').click(function(e) {
something();
});
$('#id').click(something);
$('#id').bind("click", function(e) { something(); });
Live has a slightly difference, it will bind the event for any elements added, but since you are using the ID it probably wont happen, unless you remove the element from the DOM and add back later on (with the same ID).
$('#id').live("click", function(e) { something(); });
Not sure if this one works in any case, it adds the attribute onclick
on your element: (I never use it)
$('#id').attr("onclick", "something()");
Documentation
Yes. You could write it like this:
$(document).ready(function() {
$(".button").click(function(){
// do something when clicked
});
});
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