click function is not working. I don,t know the reason
$("img[id^='down']").click(function(){
alert("hi");
});
after collect some data, a table is display in browser with down image. i need click function for that image but its not working
please help me where iam wrong
Because your target element doesn't exist on page load and it loaded using ajax, the
$("img[id^='down']").click(...
doesn't work. You should use event delegation. So your code should changed to
$(document).on('click', "img[id^='down']", function(){
alert("hi");
})
Related
One of the pages I'm working on has a modal containing a form which, when viewed on most mobile iOS devices, displays the caret/cursor in the wrong place when each input is focused. A number of people have reported this problem, including this page here.
On their advice, I was attempting to write some JS to hide the body content, etc. I'm having a hard time binding to the click event of the .new-appt and .timeslot elements.
I have tried:
A) jQuery('.new-appt').click(function(){ alert(); });
B) jQuery('.timeslot').click(function(){ alert(); });
C) jQuery(document).on('click', '.new-appt', function(){ alert(); });
D) jQuery(document).on('click', '.timeslot', function(){ alert(); });
E) jQuery(document).live('click', '.new-appt', function(){ alert(); });
F) jQuery(document).live('click', '.timeslot', function(){ alert(); });
And when pasted in the console, A through D seem to trigger fine, but not in a script block. I've also tried to placed them in a jQuery(document).ready, but that didn't seem to help.
Any advice appreciated.
You have to attach event listener to elements after these DOM elements are ready. In this case its moment when DOM in modal is ready. So basicaly you should register click event at bottom of handler that open and creates content of this modal.
I am trying to add a click event to one of the element which is nested inside of a iframe of iframe.
this is a iframe .ssueContentIframe under this, there is another iframe added as #SmartReportTabContent1 so inside of the second iframe element, I am trying to add a click event like this:
Basically, I don't know when both of iframe will be loaded and all elements will available, for future I am adding:
$('.ssueContentIframe').contents().find( '#SmartReportTabContent1' ).find('.ui-grid-selection-row-header-buttons.ui-grid-icon-ok.ui-grid-row-selected').on('click', function(){
alert('Hi');
});
But not working. what is the correct way to wait until both of iframe exist and add the click event to one of the element here?
Thanks in advance!
You need to use "contents" on the second iframe as well. to wait for it to load, you can use the "ready" event on the second iframe.
eg:
$("#SmartReportTabContent1").find("iframe").ready(function (){
// do something once the iframe is loaded
});
Update
refer to this question
Well I edit this, first do a load of the first iframe and check it loaded well, then do the function on click on the element.
$(function(){
$('#foo1').load(function(){
alert("Loaded");
$('#foo1').contents().find('iframe').contents().find('#test').on('click', function(event) {
alert("Working");
});
});
});
I tested it on my computer and worked perfectly, remember addind the library, if you don't add a library it won't work.
Just change the ID i used to your IDs.
I have a div inside my html with the id pageContent. When users click various buttons, it will load the appropriate content. When a user clicks the javaQuestions button it loads, javaQuestions.html into the div just fine. However, inside, the javaQuestions.html, I have a collapsible list, and I can't figure out a way to "bind" the li collapse/uncollapse without having the user to click TWICE. Right now what I have is:
$("#pageContent").on('click', 'li', function (){
$('.collapsible').collapsible();
});
So I guess what happens is, first the user clicks on the button, and it loads content. Then, the user clicks on any li, and it enables the "collapsible()" function, but does not uncollapse/collapse the content. Only when the user clicks a second time does it works fine. I tried adding the line $('.collapsible').collapsible(); into the event that loads the javaQuestions.html content, but it doesn't do anything. Kind of at a roadblock, any ideas?
EDIT: Here is the code that loads the content:
$("#pageContent").on('click', '#javaQuestions', function (e) {
fadeIn("#pageContent", "../java-questions/javaQuestions.html", 50);
fadeIn("#commentsContent", "../comments/comment-section.html", 500);
});
I also really want to know how this will function once I figure it out. But as you can see, the above function loads the javaquestions.html, and I can't seem to find a way to ALSO bind 'li' to be collapsible in one swoop.
Have you tried using jQuery bind method? You can bind your handler to, say, body and then you don't have to care about loading and attaching a handler after it.
Update: may be this will help:
$(document).ready(function() {
$("body").on('click', '#pageContent li', function (){
$('.collapsible').collapsible();
});
});
Use this DOCUMENT because it is dynamically loaded
$(document).on("click","li #pageContent",function(){
$('.collapsible').collapsible();
});
I have a script that does graphing, using jqplot. It works fine when the document is loaded rendering each graph using jquery's .each method. However, the problem lies when I replace the div with another one when a bar is clicked. It is suppose to render another graph in the position of the old graph. It changes the graph but does not execute the script.
The script that loads the items has this function to change all divs to graphs:
$("div.barchart").each(function(){
barChart($(this).attr("id"),$(this).attr("data-xmlurl"));
});
is there another way to do this so that it would work when a div is changed too?
Update:
Rails generates a script that is ran. However, it doesn't seem to work when I have this:
chart$=$("#<%=params[:chart_id]%>");
chart$.replaceWith("<%=escape_javascript(render :partial=>"chart_partial"}%>");
barChart(chart$.get(0).attr("id"),chart$.get(0).attr("data-xmlurl"));
Note:
For reference, the actual source code can be found in the jquery_cheats project
Perhaps you could add a listener on the parent element? Is it OK if the barChart() function gets called more than once?
Maybe something like this:
$("div.barchart").parent().on("DOMSubtreeModified", function(e) {
// (or maybe use DOMNodeInserted event instead)
$("div.barchart[id][data-xmlurl]").each(function() {
barChart($(this).attr("id"),$(this).attr("data-xmlurl"));
});
});
You can check out my jsFiddle for this here.
On the current application I'm working on, I'm stuck with version 1.5.2. To get around this, I would unbind and rebind my event and load the initialization in both "ajaxComplete" and "ready". I wasn't able to get the DOM to automatically rebind the event. Delegate is suppose to work like "on", but in my instance I still had to use the below logic.
In short, it would look something like this.
$(document).ready(function () {
InitSomethingCool();
});
$(document).ajaxComplete(function() {
InitSomethingCool();
});
function InitSomethingCool(){
$(".something").unbind('click').click(function(e) {
//Unbind and rebind click event.
alert('You clicked me!');
});
}
Reference:
http://api.jquery.com/on/
http://api.jquery.com/delegate/
I am using jQuery light box plugin (found here: http://leandrovieira.com/projects/jquery/lightbox/)
I am wondering if there is a way to detect when the image is loaded so i can reinstate my selectors?
The problem is my stript:
$('#download').click(function(e) {
e.preventDefault();
//do other stuff when a click happens
});
Does not work on the link that is loaded into the 'title' area of the lightbox.
Please Help
Thanks :)
if you're using jQuery 1.3 or later, you can use jQuery.live
$('#download').live("click", function(e) {
e.preventDefault();
//do other stuff when a click happens
});
It will attach a click handler to the #download link, even if the link created in the future.
I think you may be looking for
$('#download').live('click', function(e){
e.preventDefault();
//do other stuff when a click happens
});
#download will not be loaded into the DOM if you are creating it after the page has been loaded