I have a form with some input and select boxes, each has class="myClass". I also have the following script:
$(document).ready(function() {
$(".myClass").change(function() {
alert('bla');
})
});
I dont understand why after each change in select box or input box, this function is being called twice.
What's wrong here?
Appreciate your help!
All I can think of is that you used the same class on the form itself. if so, remove the myClass style from your form tag.
Corrected :
http://jsfiddle.net/rY6Gq/1/
Faulty one with double alert:
http://jsfiddle.net/rY6Gq/
e.stopImmediatePropagation(); is what worked for me.
$(document).ready(function() {
$(".myClass").change(function(e) {
e.stopImmediatePropagation();
alert('bla');
})
});
Its a bug,
You'd add
$("#some_id").unbind('change');
before any change call
It happens when the same class or whatever attribute you are binding also has the same name parent or child. Obviously, when you change a child, parent also gets changed (its child changes). If they have the same class or attribute, it should fire twice.
For example, in the following if you bind to "myClass", it will be called twice.
<div class="myclass">
<select class="myClass"> </select>
</div>
if this occurred in IE, it may be this bug as it was for me:
http://bugs.jquery.com/ticket/6593
updating to jQuery 1.7.1 worked for me.
For me - I had written the on change event inside a function.
Moving it to $(document).ready(function () {}); solved my case.
change like this
$(document).ready(function() {
$(".myClass").unbind('change');
$(".myClass").change(function() {
alert('bla');
})
});
It isn't: http://jsfiddle.net/nfcAS/
The only one that worked for me was unbind before the change check.
$(".select2Component").unbind();
$(".select2Component").change(function() {
//code
});
Try debugging the code in Firebug rather than alerting. It may be loosing the focus and returning it is causing the appearance of two changes when there isn't two happening
Related
I have an issue with a really simple function that is trying to fire when a text box changes. This seems to be a commonly asked question, but most of the time seems to revolve around the top $(document).ready being missing.
however this is stumping me. I have a number of other JQuery elements firing correctly, but this one doesn't seem to want to. Any ideas guys?
detection: <input type="text" placeholder="detect value" name="txt_detection" id="txt_detection">
$(document).ready(function() {
$('#txt_detection').on('change, keyup', function(){
alert "oops!";
});
});
I have it in a fiddle too: https://jsfiddle.net/hzmjjzd9/
Any help gratefully received.
You have two issues in your code. Firstly on() accepts each event in a single string that's space delimited, not comma delimited. Secondly, you're missing the brackets when you call alert(). Try this:
$(document).ready(function() {
$('#txt_detection').on('change keyup', function() {
alert("oops!");
});
});
Alternatively you can use the input event as it convers additional methods of amending the content of an input element, such as pasting using the mouse.
jQuery($ => {
$('#txt_detection').on('input', function() {
console.log("oops!");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
detection:
<input type="text" placeholder="detect value" name="txt_detection" id="txt_detection">
What if your both concerns solves by single change event as follows,
Your jsfiddle doesnt have jquery library and your alert syntax was wrong.
$(document).ready(function() {
$('#txt_detection').on('input', function() {
alert("oops!");
});
});
Here is working jsfiddle.
Give it a try, this should work.
Document-ready Document
I have a form with some input and select boxes, each has class="myClass". I also have the following script:
$(document).ready(function() {
$(".myClass").change(function() {
alert('bla');
})
});
I dont understand why after each change in select box or input box, this function is being called twice.
What's wrong here?
Appreciate your help!
All I can think of is that you used the same class on the form itself. if so, remove the myClass style from your form tag.
Corrected :
http://jsfiddle.net/rY6Gq/1/
Faulty one with double alert:
http://jsfiddle.net/rY6Gq/
e.stopImmediatePropagation(); is what worked for me.
$(document).ready(function() {
$(".myClass").change(function(e) {
e.stopImmediatePropagation();
alert('bla');
})
});
Its a bug,
You'd add
$("#some_id").unbind('change');
before any change call
It happens when the same class or whatever attribute you are binding also has the same name parent or child. Obviously, when you change a child, parent also gets changed (its child changes). If they have the same class or attribute, it should fire twice.
For example, in the following if you bind to "myClass", it will be called twice.
<div class="myclass">
<select class="myClass"> </select>
</div>
if this occurred in IE, it may be this bug as it was for me:
http://bugs.jquery.com/ticket/6593
updating to jQuery 1.7.1 worked for me.
For me - I had written the on change event inside a function.
Moving it to $(document).ready(function () {}); solved my case.
change like this
$(document).ready(function() {
$(".myClass").unbind('change');
$(".myClass").change(function() {
alert('bla');
})
});
It isn't: http://jsfiddle.net/nfcAS/
The only one that worked for me was unbind before the change check.
$(".select2Component").unbind();
$(".select2Component").change(function() {
//code
});
Try debugging the code in Firebug rather than alerting. It may be loosing the focus and returning it is causing the appearance of two changes when there isn't two happening
See this fiddle..
HTML:
<select>
<option>hey1</option>
<option>hey2</option>
<option>hey3</option>
<option>hey4</option>
<option>hey5</option>
</select>
jQuery:
$(document).ready(function () {
$('select').on('click',function(){
$("option:first",this).remove();
$(this).unbind('click');
});
});
When I run the above code in google Chrome(latest version), the first element is removed but it appends an extra element at the bottom. Why is it behaving like that.
Any ideas? pretty unexpected ..
EDIT:
This picture is for the ones who are not able to see any error..
Looks like a rendering bug in Chrome. You can't actually click on the last hey5 and the DOM doesn't actually create a second one. You can get around this via mousedown:
$('select').one('mousedown',function(){
$("option:first",this).remove();
});
jsFiddle example
I'm pretty sure it's a bug, another fix is using focus event :
$('select').on('focus', function(){
$("option:first", this).remove();
$(this).unbind('focus');
});
fiddle: http://jsfiddle.net/F8E7L/
I have a page that has a lot of JS created elements. So i wish to focus a textarea after it appear. I try to make it with help of addEventListener like so:
mytextarea.addEventListener('someEvent', function(e)
{
this.focus();
});
My problem is, that i can not found the right Event, that would be fired at the moment, when textarea get appended in the document, like here
document.getElementsByTagName('body')[0].appendChild(mytextarea);
Native JS Only please. Thank you
Have you tried this:
document.getElementsByTagName('body')[0].appendChild(mytextarea);
mytextarea.focus();
FIDDLE
this should work:
<textarea autofocus></textarea>
Put your focus in
$(document).ready(function(){
//Here
});
This mean your textarea is appended to the document.
And without jQuery :
window.addEventListener('load', function () {
//here
});
After initialize js I create new <div> element with close class and on("click") function doesn't work.
$(document).on('click', '.post-close', function () {
alert("hello");
});
but on('hover') work perfectly.
$(document).on('hover', '.post-close', function () {
alert("hello");
});
but I need to make it work on click.
It's because you're not preventing the default behaviour of the browser. Pass e into your handler and then use e.preventDefault()
$(document).on('click', '.post-close', function (e) {
e.preventDefault();
alert("hello");
});
Edit
Also, bind the handler before creating the new <div>
why not use something like
$('.post-close').click(function(){
//do something
});
If the element was added dynamically use:
$(document).on('click', '.post-close', function(){
//do something
});
edit:
like danWellman said, you can add the preventDefault IF you want to make sure no other code is executed. otherwise use the code above.
edit2:
changed the .live to .on
It's an old post but I've had a exactly same problem (element created dynamically, hover works, but click doesn't) and found solution.
I hope this post helps someone.
In my case, I found ui-selectable is used for parent element and that was preventing from click event propagate to the document.
So I added a selector of the button element to ui-selectable's 'cancel' option and problem solved.
If you have a similar probrem, check this
Try turn of libraries for parent element
You're not using stopPropagation() in parent element ?