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
Find and Remove Objects in an Array Based on a Key Value in JavaScript
Convert '1' to '0001' in JavaScript
How to Determine If Object Is in Array
Get Time Difference Between Two Dates in Seconds
How to Get the Fragment Identifier (Value After Hash #) from a Url
Regex Using JavaScript to Return Just Numbers
How to Get Elements with Multiple Classes
Is There a Built-In Way to Loop Through the Properties of an Object
Check If All Values of Array Are Equal
React-Router: No Not Found Route
JavaScript Event Handler with Parameters
What Does $(Function() {} ); Do
How to Measure a Time Spent on a Page
JavaScript - Get Portion of Url Path
How Do Browsers Pause/Change JavaScript When Tab or Window Is Not Active