Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery onClick event

Cant seem to get this working. Any help would be great. I am somewhat of a beginner with jquery.

$('#add_button').click(function () {
    $("#add_button").hide();
});

Here is the HTML

<input id='add_button' type='submit' class='add_now' value='' />

Seems simple enough, but clicking on the input does nothing. Is there a different method for targeting inputs? Thanks.

like image 919
Earl Higgins Avatar asked Apr 17 '11 23:04

Earl Higgins


People also ask

How can set click event 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.

What is difference between click and Onclick in jQuery?

So, using a string that is tied to a function, onclick produces an attribute within the binded HTML tag. . click, on the other hand, attaches the function to the property element.

Is jQuery click deprecated?

click() shorthand is deprecated at jQuery 3 on() and . trigger() methods can set an event handler or generate an event for any event type, and should be used instead of the shortcut methods.

What is event in Onclick?

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.


2 Answers

Make sure you include jQuery before your script:

<script src="http://code.jquery.com/jquery-latest.js"></script>

And put your code inside a document-ready function:

$(document).ready(function () {
    $('#add_button').click(function () {
        $("#add_button").hide();
    });
});
like image 94
David Tang Avatar answered Oct 25 '22 02:10

David Tang


You should add return false; to prevent the browser's default action.

like image 27
SLaks Avatar answered Oct 25 '22 03:10

SLaks