Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to automatically get the content in #mydiv when I load a page. Since the content is dynamically, so I just use .on(). It works when I click the #mydiv.
But can it work without clicking the #mydiv? It will be nice if it loads automatically.
$( "#mydiv" ).on( "click", function() {
alert( $( this ).text() );
});
I have tried "load", but didn't get any luck...
Why do you need to do this specifically with jQuery? Browsers already has means to call some JavaScript code on page load:
<body onload="alert($('#mydiv').text())">
Or,
<body onload="alert(document.getElementById('mydiv').innerText)">
Above two will do the same.
Any code you specify between a <script> tag will get executed just when the script tag is loaded.
<script>alert($('#mydiv').text());</script>
And if you want to call a function when innerHTML of an element is changed, you should take a look at these questions:
Fire event when inner html in div changed
Listen to changes within a DIV and act accordingly
jQuery watch for domElement changes?
Try this:
$(function(){
alert($( "#mydiv" ).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="mydiv" > Some Text </div>
do you mean this?
$(function() {
alert( $( '#mydiv' ).text() );
});
that will alert the contents of #mydiv as soon as the page finishes loading and is fully available.
You probably meant trigger()
eg.
$( "#mydiv" ).on( "click", function() {
alert( $( this ).text() );
});
$( "#mydiv" ).trigger('click');
Try the jQuery trigger method
Any event handlers attached with .on() or one of its shortcut methods are triggered when the corresponding event occurs.
$( "#foo" ).on( "click", function() {
alert( $( this ).text() );
});
$( "#foo" ).trigger( "click" );
In this example the foo click event is triggered.
$(document).ready(function()
{
alert($("#mydiv").text())
})
Related
I've written a jQuery function which updates a div upon clicking another div. However, it is only firing on the second click each time and I'm rather confused.
How can I fix this?
$(document).ready(function(){
$(".block-swatch").click(function(){
$("#refresh-feefo").load(window.location.href + " #refresh-feefo" );
});
});
You should consider using .on as it allow to bind event from a parent and if your DOM element #refresh-feefo isn't ready, it will still work.
$(document).ready(function(){
$("body").on('click', '.block-swatch', () => {
$("#refresh-feefo").load(window.location.href + " #refresh-feefo" );
});
});
I used body , but be careful:
Hierarchical selectors can often be avoided simply by attaching the handler to a more appropriate point in the document. For example, instead of $( "body" ).on( "click", "#commentForm .addNew", addComment ) use $( "#commentForm" ).on( "click", ".addNew", addComment ).
Read Event performance section here for more: https://api.jquery.com/on/
I'm a total beginner and I'm about to finish my first website.
But I just cant get the last button to work. Its the simplest task but this one is just not working.
$( document ).ready(function() {
alert("mailto script loaded!");
})
//eventhandler send-button
$('#send').onclick(function() {
alert( "Handler has beend called." );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="buttonsend">
<button class="button button-pill button-flat-action" id="send">Senden</button>
</div>
Can you guys give me a hint where the problem is?
Thank you very much in advance. I googled everything but i unterstand basically what i need to do. I just cant see where i am going wrong.
The jQuery event is called click not onclick. Change to:
$('#send').click(function() {
Depending on where the JavaScript is placed on the page, you may need to wrap it in a DOM ready handler.
$( document ).ready(function() {
$('#send').click(function() {
alert( "Handler has beend called." );
});
});
Aside from putting the handler in the document ready, you should fix your syntax too. You need to use .on() to handle an event which is the first parameter to the method. Documentation here: https://api.jquery.com/on/
//alert if the js-script is succesfully loaded
$( document ).ready(function() {
alert("mailto script loaded!");
//eventhandler send-button
$('#send').on('click',function() {
alert( "Handler has beend called." );
});
})
Working Example:
http://jsfiddle.net/5xn5tpq4/
The code in the javascript pane is already wrapped with the ready handler.
somehow my simple jQuery Statement is not working when I include it in my "head"-element topic like :
<script src="js/test.js" type="text/javascript"></script>
but when I use it in my "body"-element it just works fine like
<script>
$( "#testid" ).click(function() {
alert( "Handler for .click() called." );
});
</script>
I checked already with firebug that my test.js file gets included on my site correctly. So what do I wrong ? The test.js file looks exactly like the "script" part above just without the script-tags.
Any idea is appreciated :)
You're forgetting to tell jQuery to wait until the DOM ready before firing up your code:
$(function() {
$( "#testid" ).click(function() {
alert( "Handler for .click() called." );
});
});
When you include your code in the body tag after the target element your code will work. However, if you include it in the body section before the target element or in the head section your code would not work. It is therefore recommended to always wrap your code in DOM ready. DOM ready can be written in different ways:
All three of the following syntaxes are equivalent:
$( document ).ready( handler )
$().ready( handler ) (this is not recommended)
$( handler )
Check out the documentation: http://api.jquery.com/ready/,
and the tutorial: http://learn.jquery.com/using-jquery-core/document-ready/
Feels like I rip of Rajaprabhu but:
<script>
$(document).ready(function (){
$( "#testid" ).click(function() {
alert( "Handler for .click() called." );
});
});
</script>
Try this..
<script>
$(document).ready(function(){
$( "#testid" ).click(function() {
alert( "Handler for .click() called." );
});
});
</script>
Also make sure your script has been added after jquery scipt.
I would like to trigger a javascript function when a specific URL is available on the page.
The url looks like:
I would like to use jQuery to detect the url and launch an event.
I have come this far:
$( 'a[href="https://www.mypage.com/my-page/details.jsp"]' ).bind( "click", function() {
alert( "yoehoe" );
});
But it doesn't trigger the alert on a click. on the specific href. Can anyone help me on this one?
You need to wrap the code in document.ready to ensure that event gets bind to respective elements once they are there on page.like this:
$(document).ready(function(){
$( 'a[href="https://www.mypage.com/my-page/details.jsp"]' ).bind( "click", function() {
alert( "yoehoe" );
});});
Demo
Alternatively, You can also use .on() with .click if dom is generated dynamically.like this:
$(document).on("click",'a[href="https://www.mypage.com/my-page/details.jsp"]',function() {
alert( "yoehoe" );
});
I have two span elements in a page. when I call a jquery double click function on both then the function is called only on first element. I am using the following code:
<span id="shiftTime_1">1</span>
<span id="shiftTime_2">1</span>
and jquery function is:
$("[id^='shiftTime_']").dblclick(function() {
alert("hello");
});
when I double click on the element Id of shiftTime_1. then the function works fine. But when I double click on element Id of shiftTime_2 then this function does not respond.
Please help.
Thanks
Use .on if you plan to add elements dynamically (e.g. by using $( "body" ).append( "<span id='shiftTime_2'>1</span>" ); )
$( "body" ).on( "dblclick", "span", function() {
alert( "This works also with dynamically added elements" );
} );
try use inside $(document).ready()
$(document).ready(function(){
$("[id^='shiftTime_']").dblclick(function() {
alert("hello");
});
});
When I try the code, it works just fine:
http://jsfiddle.net/Guffa/pfQfK/
Check if there is something different from your code.