Div won't fade out/fade in - javascript

I am trying to get a div to fade out and in on button click. It works on jfiddle but for some reason it is not working on my asp.net page:
$('#btn').click(function(e){
$('#fancy').fadeOut('slow', function(){
$('#fancy').fadeIn('slow');
});
});
<div>fade div</div>
<div id="fancy">Fancy Div</div>
http://jsfiddle.net/3XwZv/1859/
my asp.net page:
<html>
<head>
<script type="text/javascript" src="../javascript/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../javascript/jquery-ui.js"></script>
<script type="text/javascript" class="init">
$('#btn').click(function (e) {
$('#fancy').fadeOut('slow', function () {
$('#fancy').fadeIn('slow');
});
});
</script>
</head>
<body>
<div>Fade div</div>
<div id="fancy">Fancy Div</div>
</body>
</html>

If you put your jQuery code inside the head section and try to manipulate the DOM elements it won't work as at that time your HTML document isn't loaded yet. So you have two ways to resolve this issue.
First : Use $(document).ready() function and put your code inside it. So whenever your document is ready, $(document).ready() event will be fired.
$(document).ready(function () {
$('#btn').click(function (e) {
$('#fancy').fadeOut('slow', function () {
$('#fancy').fadeIn('slow');
});
});
});
Second : Put your jQuery code at bottom of your page.

Can you share the page you have this implemented on? Hard to know what's going wrong without seeing the failing code. My first guess would be that jQuery isn't running on your page.
In the meantime, you could try throwing a log inside of your click function to see if that's firing.
$('#btn').click(function(e){
console.log('Click fired');
$('#fancy').fadeOut('slow', function(){
$('#fancy').fadeIn('slow');
});
});

Looks like you may have not done the binding:
try this code
$(function(){
$('#btn').click(function(e){
console.log('Click fired');
$('#fancy').fadeOut('slow', function(){
$('#fancy').fadeIn('slow');
});
});
}

Related

JQuery function .load not working

I have the next code. What am trying to do is when i refresh or open the page, to add the class intro. But my main problem is in the part :
$("body").load(function(){
I want when the page is opened, then to add the class .intro. Instead of body, i have also tried html, and still doesn't work.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("body").load(function(){
$("p").addClass("intro");
});
});
</script>
<style>
.intro {
background-color:green;
}
</style>
</head>
<body>
<img src="https://k32.kn3.net/taringa/1/9/3/9/4/7/32/johnny_te_toco/330x330_248.gif" width="304" height="236">
<p><b>Note:</b> Depending on the browser, the load event may not trigger if the image is cached.</p>
</body>
</html>
You should not use load() for this purpose.
load() function description : Load data from the server and place the returned HTML into the matched element.
You need just ready function to achieve that :
$(document).ready(function(){
$("p").addClass("intro");
});
//OR
jQuery(function($){
$("p").addClass("intro");
});
Hope this helps.
Try this instead:
$(document).ready(function(){
$(window).load(function(){
$("p").addClass("intro");
});
});
Load event attached to the window. If that is what you meant? Load on a DOM element like that doesn't make much sense.
Whilst this should work, you could pull it out of your ready() event.
$(document).ready(function(){
// Nothing to see here...
});
$(window).load(function(){
$("p").addClass("intro");
});
This waits for the load of the window, but if you just want to add the class as soon as possible when the DOM is ready, well, you won't need the load() at all:
$(document).ready(function(){
$("p").addClass("intro");
});
Bonus: You should use on() to attach all events going forward.
$(window).on('load', function(){
$("p").addClass("intro");
});
on() is the preferred method for events in future jQuery versions, as older methods are deprecated.
All the best.
Put your code inside $(document).ready callback.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").addClass("intro");
});
</script>
<style>
.intro {
background-color:green;
}
</style>
</head>
<body>
<img src="https://k32.kn3.net/taringa/1/9/3/9/4/7/32/johnny_te_toco/330x330_248.gif" width="304" height="236">
<p><b>Note:</b> Depending on the browser, the load event may not trigger if the image is cached.</p>
</body>
</html>
$(window).on('load', function(){
$("p").addClass("intro");
});

jQuery just not working

For some reason my jQuery script just doesn't want to work, and it's really really simple, I've taken everything away except the actual jquery script, it's really annoying please help.
Thank you!
<!doctype html>
<html>
<head>
<script src="jquery.js"></script>
<script>
$("#click").click(function () {
alert("works!");
});
</script>
</head>
<body>
Button
</body>
</html>
P.S. jQuery JS file is valid.
Change your code to this. It ensures that the JavaScript is executed once all your HTML elements have been loaded to the page:
$(document).ready(function () {
$("#click").click(function() {
alert("works!");
});
});
However, you should move your script tags to the bottom of the page, to ensure that they do not block. This will also mean you don't actually need the $(document).ready callback.
You need to wrap your code within
$(function(){
// your code here which relies on DOM elements
});
Otherwise you DOM has not been loaded and events will not be bound to elements.
Wrap your functions into the $(document).ready()
$(document).ready(function(){
$("#click").click(function() {
alert("works!");
});
});
Add your code inside document.ready like this
$(document).ready(function () {
$("#click").click(function () {
alert("works!");
});
});
or add your code at the end of the document like this
<body>
Button
// Other elements...
<script type="text/javascript">
$("#click").click(function () {
alert("works!");
});
</script>
</body>
$(document).ready(function(e) {
//TO DO your code goes here
});
Just change your script tag to the "/body" tag.
That will let the browser create the DOM and read script's one by one!
Cheers!
Try this:
$("#click").live("click",function() {
alert("works!");
});

Show javascript alert and do other actions?

Here's my code and nothing is happening:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-git.js"></script>
<script>
$(".clickMe").click(function() {
alert('hi!');
// Do other stuff
});
</script>
</head>
<body>
<div class="clickMe">Click Me!</div>
</body>
</html>
However, nothing happens when you click the "clickMe" div.
Thanks!
Let the document be ready
$(function(){
$(".clickMe").click(function() {
alert('hi!');
// Do other stuff
});
})
The way you had it, the div is not yet available (the DOM is not loaded) so, no click handler is added. You need to wait for document to be available. The .ready() is to be used for that.
$(document).ready(function() {...})
$(function() {...}) - Shortcut
Try wrapping the jQuery code inside this:
$(document).ready(function() {
// Handler for .ready() called.
});
When you execute the existing code in the head, the clickMe div does not exist.
Try this:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".clickMe").click(function() {
alert('hi!');
// Do other stuff
});
});
</script>
</head>
<body>
<div class="clickMe">Click Me!</div>
</body>
</html>
You by comparing the differences you will find out the solution. Basically is this line: $(document).ready(function(){});
The DOM is not loaded yet when your JavaScript code is executed. You have to add the code to the .ready() callback. Then the code is executed when the browser parsed the HTML and jQuery can find the elements:
$(function() {
$(".clickMe").click(function() {
alert('hi!');
// Do other stuff
});
});
$(function(){..}); is shorthand for $(document).ready(function(){...}).
DEMO
The script is running before the .clickMe element even exists. Wrap your code in a dom ready callback:
$(function() {
$(".clickMe").click(function() {
alert('hi!');
// Do other stuff
});
});
Another solution would be moving your script below the element definition but using the dom ready event is much cleaner.
The issue is that you are attaching the event handler before the element is rendered to the DOM. Try this instead:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-git.js"></script>
<script>
$(document).ready(function() {
$(".clickMe").click(function() {
alert('hi!');
// Do other stuff
});
});
</script>
</head>
<body>
<div class="clickMe">Click Me!</div>
</body>
</html>
The code passed into the $(document).ready function will be executed once the page is entirely loaded - so you can safely attach events to yet-to-be-created elements.
http://api.jquery.com/ready/
You're asking jquery to find your clickMe div before it exists. It's down lower on the page and the browser has not loaded it yet. A simple fix is to set up your click handler during the document ready event:
<script>
$(document).ready(function() {
$(".clickMe").click(function() {
alert('hi!');
// Do other stuff
});
});
</script>

jQuery class selector and click()

I'm currently trying to get an alert to happen when something is clicked. I can get it working in jsFiddle, but not in production code:
jsFiddle example that works (jQuery 1.5 loaded)
HTML (in case jsFiddle is inaccessible):
<!DOCTYPE HTML><html><head><title>Test</title></head>
<body> <h1>25 Feb 2011</h1><h3>ABC</h3><ul>
<li class="todoitem">Test—5 minutes</li> </ul>
</body></html>
Javascript:
$(".todoitem").click(function() {
alert('Item selected');
});
Non-working production example:
<!DOCTYPE HTML><html><head><title>Test</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(".todoitem").click(function() {
alert('Item selected');
});
</script>
</head>
<body>
<h1>25 Feb 2011</h1><h3>ABC</h3><ul><li class="todoitem">Test—5 minutes</li></ul>
</body>
</html>
Safari's Inspector indicate that jQuery is being loaded correctly, so that's not the issue. As far as I can tell, these two pieces of code are essentially identical, but the latter isn't working. Can anyone see what I've done wrong?
You need to wrap your code in $(document).ready()
This will work
$(document).ready(function(){
$(".todoitem").click(function() {
alert('Item selected');
});
});
JSfiddle does this for you automatically
Wrap the code inside
$(function (){
})
And you have the working code
$(function (){
$(".todoitem").click(function() {
alert('Item selected');
});
})

Jquery show div when link gets clicked

Im trying to show/hide a div using jquery when a link gets clicked. I put this in my head section:
<script type="text/javascript">
$("#attach_box").click(function {
$("#sec_box").show()
});
</script>
I have a link that looks like this:
+ Add a Postal Address (If Different)
And a div that looks like this:
<div id="sec_box" style="display: none;">
Hello world!!
</div>
This doesn't work and I can't figure out why. Any ideas?
You need to attach the click handler in the document.ready in order to make sure that the DOM has been loaded by the browser and all the elements are available:
<script type="text/javascript">
$(function() {
$('#attach_box').click(function() {
$('#sec_box').show();
return false;
});
});
</script>
Also you forgot to put parenthesis () next to the anonymous function in the click handler.
Chances are the DOM isnt fully loaded yet.
<script type="text/javascript">
$(document).ready(function()
{
$("#attach_box").click(function() {
$("#sec_box").show()
});
});
</script>
put that in your head and put your initialization code in there.

Categories

Resources