Why Is This Jquery Click Function Not Working

Why is this jQuery click function not working?

You are supposed to add the javascript code in a $(document).ready(function() {}); block.

i.e.

$(document).ready(function() {
$("#clicker").click(function () {
alert("Hello!");
$(".hide_div").hide();
});
});

As jQuery documentation states: "A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute"

jQuery .click() not working?

Try this:

$(document).on('click', '.download', function(){ 
// Your Code
});

jquery click function not working even after using $(document).ready(function() {}); and even after using $(function) block

you can also use .on() click event.

$(document).on("click", "#obj1", function(){
alert("clicked");
});

Click event not working but works when put in console

You might be missing the JQuery library in your HTML file. Your code works well. Also if they are created dynamically you can use this code below which will add an event listener to the dynamically created element and attach it to the document object.

$(document).on('click','.cart_minus', function () {    alert("hai");});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><p>Men's Shirt (on hanger) <span class="cart_minus"> - </span><span class="cart_add"> + </span> <label class="text_right"> 2 Suits </label> </p><div class="total"> Total <span>£22.00</span></div> <a href="" class="order_now"> Order now </a>

Jquery .click function not working for button

Because type="submit" also triggers submit event when it is inside form so it is overriding click event.
So either try to remove type "submit" and put "button" write like this

<input type="button" value="something" name="resetbtnB" id="resetbtnB"/>

Or write jquery function .submit

Docs : https://api.jquery.com/submit/

Demo : https://jsbin.com/morisob/9/edit?html,js,output

$(this) is not working in my jQuery click function

When you invoke a function in a onclick attribute, it is actually invoked by the window object, so this will become the window object itself, not the button.

If you want the this become the button itself, you will have to use apply method of the function.

For example:

function somefunc(arg1, arg2){
console.log("This is ", this);
console.log("Arg1: ", arg1);
console.log("Arg2: ", arg2);
}

In onclick

<button onclick="somefunc.apply(this, [1, 2])">Button</button>


Related Topics



Leave a reply



Submit