This question already has answers here:
What's the effect of adding 'return false' to a click event listener?
(16 answers)
Closed 8 years ago.
When I try to add this line of code :
onclick="document.getElementById('part3').style.display='';return false;"
Into a link it disables the other function for some reason.. This is the code in which i would like to add the above code:
a href="javascript:DecreaseQuantity('PROD_VK_1.4')">
Works fine until i add the code, then only the onclick function works not the javascript part.
How can I fix this?
Try this,
<a href="javascript:DecreaseQuantity('PROD_VK_1.4')" onclick="doFunction()" />
function doFunction() {
document.getElementById('part3').style.display='';
return false;
}
or:
function doFunction() {
if ($("#part3").css("display", "")) {
return false;
} else {
return true;
}
}
Related
This question already has answers here:
How to combine keypress & on click function in JavaScript?
(5 answers)
Closed 5 years ago.
I have the following two functions:
$("input").keypress(function(event) {
if (event.which == 13) {
//code
}
});
$('#login_submit').click(function () {
//code
});
The code which is being used in the functions are EXACTLY the same code, basically code duplication. So i was wondering if there is a way to combine these functions with an AND statement?? There is an question on Stack but it aks for an OR logic. If you want the OR solution here
EDIT: I am trying to make somthing like an explorer. Now I want to hold "shift" and click on an "input" to mark them all. So i need the "onclick" on my input element to be true AND my keydown to be true.
After your edit, I believe what you are looking for is something like the following.
var shift_hold = false;
$(document).keydown(function(e) {
if(e.which === 16) {
shift_hold = true;
}
})
$(document).keyup(function(e) {
if(e.which === 16) {
shift_hold = false();
}
})
$('input').click(function() {
if(shift_hold) {
//your code here
}
})
I want to hold "shift" and click on an "input" to mark them all. So I need the "onclick" on my input element to be true AND my keydown to be true.
Within the click event, you can check if a key is also pressed down (click and key-is-down).
You can use the event object to see which keys are pressed. shift/control/alt have their own explicit properties.
Example:
$("input").click(function() {
console.log(event.shiftKey)
if (event.shiftKey) {
$(this).addClass("selected")
} else {
$(this).removeClass("selected")
}
// could use .toggleClass("selected", event.shiftKey) here,
// shown expanded for clarity
});
.selected {
border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Shift-click input to select, click to unselect
<input type='text'>
<input type='text'>
This question already has an answer here:
Click event not working for dynamically added Li->span->id
(1 answer)
Closed 6 years ago.
If i add the search bar right away, it works, the console returns the value of the search field every time i press enter, but if i add the search bar after the image was clicked it doesn't work.
http://codepen.io/Nadaga/pen/QEVaGA
$('#glass-image').on('click', function() {
$('#main').html('<input id="search-field" type="text" placeholder="Search"></input>');
})
$('#search-field').keypress(function (e) {
if (e.which == 13) {
console.log($('#search-field').val());
return false;
}
});
Since, the search-field is dynamically created, It has to be added like this (on the $document),
$(document).on('keypress', '#search-field' ,function(e) {
if (e.which == 13) {
console.log($('#search-field').val());
return false;
}
});
$('#glass-image').on('click', function() {
$('#main').html('<input id="search-field" type="text" placeholder="Search"></input>');
$('#search-field').keypress(function(e) {
if (e.which == 13) {
console.log($('#search-field').val());
return false;
}
});
})
Click event only works if the element already exist in html code. So the click event doesn't fire.It won't consider the new element which is created dynamically after the page loaded. Dynamic elements are created with the help of javascript or jquery(not in html).
source: https://stackoverflow.com/a/29674985/1848140
This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 7 years ago.
i am trying to add a new span of tag inside tags div.
my problem is that the code under span[id="newtag"] doesnt
work. how can i make this code work?
$(document).ready(function () {
$('#tags').on("click", function () {
$(this).append('<span id="newtag" contenteditable="true"></span>');
$(this).children(":last").focus();
});
$('span[id="newtag"]').keypress(function (k) {
if (k.which == 13 || k.which == 32) {
$(this).append('gfskjsokdfla');
}
});
});
Use event delegation for created dymanically dom elements.
$('#tags').on('keypress', 'span[id="newtag"]', function(k){
if (k.which == 13 || k.which == 32) {
$(this).append('gfskjsokdfla');
}
});
This question already has answers here:
Click source in JavaScript and jQuery, human or automated?
(4 answers)
Closed 8 years ago.
I have an image and code
$(thisSrc).click();
How I can to distinguish physic mouse click by user
$(thisSrc).on("click", function () {
});
from program by javascript?
$(thisSrc).click();
There is isTrigger key in event when it's triggered.
$(el).click(function(e) {
if (e.isTrigger) {
// triggered
} else {
// clicked by hand
};
});
One way would be like this:
var realClick = true;
realClick=false;$(thisSrc).click();
$(thisSrc).on("click", function () {
// stuff
realClick=true;
});
This question already has answers here:
Cancel the keydown in HTML
(6 answers)
Closed 9 years ago.
I am not sure how to do 'keydown' with pure JavaScript.
Here's how I did it with jQuery, but is there a way to do the same thing with pure JS also ?
I just wanna check if there is a way and to learn how to do it, as well as to see difference and length of code. Thank you.
$('body').on('keydown', function(e) {
if (e.keyCode === 27) return false;
});
EDIT: It is used to disable "Esc" button!
I don't like setting the listeners as attributes, so I add even listeners like this:
var ele = document.getElementById('test');
ele.addEventListener('keydown', function() {
//code
}, false);
Here you go
document.body.onkeydown = function(e) {
if (e.keyCode === 27) return false;
}
As you can see jQuery is shorter but this will execute less code
In pure javascript it is done this way
document.body.onkeydown=function(e){
key=e.keyCode || e.charCode|| e.which; //cross browser complications
if(key===27){
return false;
}
}
Hope it helped, it is the same script as the JQuery one you provided
Try this,
HTML
<body onkeydown="YourFunction(event)">...</body>
JS
function YourFunction(e)
{
var unicode=e.keyCode || e.charCode|| e.which;
if(unicode == 27)
return false;
else
{
//Your code...
}
}