On change event not fired with dynamically loaded data - javascript

I am dynamically loading data in the input type text and triggering alert if the value of the text box is changed. But my code does not seem to work. Please provide suggestions.
visit this page for code: http://jsfiddle.net/NbGBj/103/
I want the alert to be shown when the page is loaded

You should remove your " around document
$("document").ready(function(){
...
should be
$(document).ready(function(){
...
or use the shortcut
$(function(){
...
From the official documentation:
All three of the following syntaxes are equivalent:
- $(document).ready(handler)
- $().ready(handler) (this is not recommended)
- $(handler)
http://api.jquery.com/ready/
But I guess what you want is the keyup event
$(function () {
$("#upload").val("sample");
$("#upload").keyup(function () {
alert($(this).val());
});
});
And for better readability, you can use chaining
$(function () {
$("#upload").val("sample").keyup(function () {
alert($(this).val());
});
});
http://jsfiddle.net/BXNkq/

You can just fire the change function directly after you set the value in the text box like this:
....
$("#upload").val("sample");
$("#upload").change();
....

Related

What is the proper way of binding REMOVE event listener to dynamically created elements in JQuery

My goal is to perform a certain operation each time an element with a certain id or class is deleted from DOM. Prior to this, I've been successfully using a standard syntax for binding click events on dynamically created elements like this:
$(document).on('click', '.someClass', function(e) {
alert("Div was clicked");
});
Inspired by this post, I tried to do the same thing for REMOVE event listener, and failed.
Here is my jsFiddle.
Any hints on how to make this work? And maybe what I am trying to do is fundamentally wrong, and I should come up with some entirely different logic ?
http://jsfiddle.net/wphtjw1o/
This is the functionality of Jquery UI script, so you have to include two scripts Jquery and Jquery UI to make it work.
$(function() {
$('<div id="DivToBeRemoved">DIV TO BE REMOVED</div>').prependTo('body');
$("#DivToBeRemoved").on("remove", function () {
alert("Element was removed");
});
$(document).on('click', '#DivToBeRemoved', function(e) {
alert("Div was clicked");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<button onclick="RemoveDiv();">Click here to remove div above</button>
<script type="text/javascript">
function RemoveDiv() {
var d = $("#DivToBeRemoved");
d.remove();
}
</script>
If I got you correctly, you trying to write custom event (remove event in your case).
For calling custom event you don't need any library, you need to trigger that event, like so:
function RemoveDiv() {
var d = $("#DivToBeRemoved");
// d.remove();
d.trigger('remove'); // <--
}
Read more about trigger: .trigger()
Also check this question here on SO: Custom events in jQuery?
jsfiddle

click on a link using jQuery on page load

I have a link with a script in that opens a view of products. I don't have access to the html, and I need to have this view open on default. Until the behavior is fixed the correct way, which will take long time unfortunaly, I want to automatically make the link click on pageload.
I tried with the script below first - but since the link itself triggers a pageload, this obviously just keeps firing over and over again.
So how do I do this with jquery or vaniall JS, is this even possible?
Script:
$(document).ready(function () {
$('.action')[0].click();
});
My link:
Toon alle
$(document).ready(function () {
$('.action:first').trigger("click");
});
to manually trigger an event u need to use trigger() ,click is to only assign a handler
$(document).ready(function () {
$('a.action').click(function(){
... handler code
});
$('a.action').trigger("click");
});
The click() method triggers the click event.
Syntax :
Trigger the click event for the selected elements:
$(selector).click();
http://www.w3schools.com/jquery/event_click.asp (example)
In your case :
$(document).ready(function() {
$('.action:first').click();
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Toon alle
Edit :
If you don't want to trigger in a specific page. do like this.
$(document).ready(function () {
if (!location.href.indexOf("someurl") > -1)
$('.action:first').click();
});
Can you tell me why you add [0], if you need to add it for first element then use following code :-
$(document).ready(function () {
$('.action:first').trigger("click");
});
otherwise you can directly use these :-
$(document).ready(function () {
$('.action').trigger("click");
});
Change your link tag with following :-
Toon alle

jQuery "Chosen" on-filter event?

I want to run a function whenever the search input box is changed in chosen (jquery dropdown plugin).
But unable to find such event neither in their documentation nor on the net. Does anyone have experienced or have idea how to call some code whenever search is done in Chosen?
I tried placing the jquery .on('change' event on the textbox but it didn't work.
I wrote
$(".chosen-search input").on("change", function(e) {
//console.log($(this));
});
I had to "double" wrap the on to get the input event to fire on the Chosen text field search box:
$('#my-chosen-input').on('chosen:showing_dropdown', function() {
$('.chosen-search input').on('input', function() {
// The text has changed, do something...
});
});
$(".chosen-search").change(function(){
console.log("changinn");
});
This should work
Chosen js filters dropdown list onkeyup event. This snippet would work better:
$(document).on('keyup', '.chosen-search input', function() {
console.log('filtered');
})

Trigger the change of text in text box

I am unable to trigger the change of the text in the current scenario.
If i click on the button i am changing the content of the text. So how can i trigger the change of the text which is caused by external event.
For example in a click of a button I can able to fill any text in the textbox, able to paste texts and using mouse and keyboard. In these all scenarios and scenarios like these also I want the event to be fired.
I have already tried the following.
$('#text_id').on('change', function () {
alert('This is not trigged when i changed the text');
});
The alert should come when the text is changed. There might be many possible sources to change the text.
This is the Fiddle i created.
Thanks in advance.
This is my updated fiddle
Updated Fiddle - http://jsfiddle.net/upa2A/1/
$(document).ready(function () {
$(document).on('click', "#button_id", function () {
$('#text_id').val('TExt changed')
});
$(document).on('change', "#text_id", function () {
alert('This is not trigged when i changed the text');
});
});
Issues in original Fiddle -
Code has to be inside $(document).ready()
Missing () after function
Incorrect use of $.on (Read more about it on http://api.jquery.com/on/)
Edit based on comment from asked -
Updated Fiddle - http://jsfiddle.net/upa2A/4/
$(document).ready(function () {
$(document).on('click', "#button_id", function () {
$('#text_id').val('TExt changed').change();
});
$(document).on('change', "#text_id", function () {
alert('This is not trigged when i changed the text');
});
});
More edit based on comments thread -
From http://api.jquery.com/change "but for the other element types the event is deferred until the element loses focus."
Since, when changing via button click, text box wasn't focussed, the trigger does not happen automatically.
I saw your code in jsfiddle. you missed in some brackets and semicolon . otherwise ur code write . pls check jquery library code.
$('#button_id').on('click',function()
{
$('#text_id').val('TExt changed');
}
);
$('#text_id').on('change',function() { alert('This is not trigged when i changed the text');
} );
Try the below code. But for this you should use jQuery. It'll raise the event whenever the text changed.
$('#text_id').on('input propertychange paste', function() {
alert('Text Changed');
});
the working fiddle is here
This event surely raised on text change. If it is not useful for you surely someone else will be happy for this.
var txtVal = '';
setInterval(function() {
if ($("#text_id").val() != txtVal) {
txtVal = $("#text_id").val();
alert("Text Changed");
}
}, 10);
working fiddle is here

jQuery .change() not triggering on select - example from api.jquery.com

I copied this example from api.jquery.com
$('.target').change(function() {
alert('Handler for .change() called.');
});
I also tried:
$('#target').change(function() {
alert('Handler for .change() called.');
});
I copied it into my .js file, which is included in my html. I have another jQuery function in there (beginning $(window).load(function () { ..."
The other function is working. But the above simple function is not.
The form looks like this:
<form>
<select id="target" class="target">
<option name="pp" value="6">6</option>
<option name="pp" value="12">12</option>
</select>
</form>
I want to just use id, but I added class just for testing. But neither works.
Why doesn't this simple function work? Do I need to do anything else to connect the change event to the function? I am new to jQuery, but I know that in javascript I would have to have the onchange event of the form call the function in order for something to happen.
EDIT: Ok, here is EVERYTHING in my included .js file. As you can see, just one other function. Is it interfering?
Also, I have only 1 form on the page, which you see above. I am going to have it change the number of results shown per page (6 or 12).
$(window).load(function() {
$("img.gall_img").each(function() { // iterate through all img of class gall_img
var imgWidth = $(this).width(); // "$(this)" to access jQuery width() func
var divWidth = $(this).closest('div').width();
//alert(imgWidth + " and " + divWidth); // for debugging
if(imgWidth > divWidth) {
var position = Math.round(-1 * ((imgWidth/2) - (divWidth/2))); //
position = position + "px"; // make position format "123px".
$(this).css("left", position); // "$(this)" to access jQuery css() func
}
});
});
$("#target").change(function() {
alert('Handler for .change() called.');
});
Put the code inside the $(window).load function, like so:
$(window).load(function () {
//whatever code is already here
$('.target').change(function() {
alert('Handler for .change() called.');
});
}); //end of $(window).load function
$(window).load(function () tells the browser to only run that code after the entire page is loaded INCLUDING images. Traditionally with jQuery you will see the $(document).ready(function() {... which tells the browser to not process that code until after the page is loaded (not including images)
IF you don't need to wait for the images to load to run your jQuery then you could replace $(window).load(function () { with $(document).ready(function () {
Cheers!
Try placing your .change(..) inside a .ready(..) function like this:
$(document).ready(function () {
$('#target').change(function() {
alert('Handler for .change() called.');
});
});
I've tried this code in jsFiddle => http://jsfiddle.net/GjScg/
And this code should work, is there any other javascript that is bounded on the Change event? Perhaps there's some other code around, that causes errors?
Are you sure, it doesn't work?
Here is a working link to jsfiddle: http://jsfiddle.net/ayezutov/7pEP4/
UPDATE:
Both versions work: http://jsfiddle.net/ayezutov/7pEP4/1/

Categories

Resources