W3 Schools example not working?

I'm trying to figure out this example by W3 Schools, but as far as I can tell, it's not working. Could someone steer me clear if I'm missing something?

<!DOCTYPE html>
<html>
<head>
<script src=""></script>
<script>
$(document).ready(function(){ $("#div1").on("click", function(){ $(this).css("background-color", "pink"); }); $("#div2").live("click", function(){ $(this).css("background-color", "pink"); });
});
</script>
</head>
<body>
<h4>This example demonstrates how to achieve the same effect using on() and live().</h4>
<div>This is some text. <p>Click to set background color using the <b>on() method</b>.</p>
</div><br>
<div>This is some text. <p>Click to set background color using the <b>live() method</b>.</p>
</div>
</body>
</html>

Fiddle:

Source:

1

5 Answers

It is because jQuery.fn.live(..); has been deprecated in version 1.7 and completely removed in version 1.9.

You are using jQuery 1.12.2, the method jQuery.fn.live(...); does not exist in this version of jQuery.

To get jQuery.fn.live(...); to work you must change the script element to this:

<script type="text/javascript" src=""></script>

If you would like to use the newest version of jQuery use this instead:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src=""></script>
<script> $(document).ready(function() { $("#div1").on("click", function() { $(this).css("background-color", "pink"); }); });
</script>
</head>
<body> <div> Hello World! <p>Click me to make me pink!</p> </div>
</body>
</html>
2

As of jQuery 1.7, the .live() method is deprecated.

In your fiddle you're using v1.12.2

Same with w3c:

src="">

So change the src to:

src=""

and it works

2

Use of the .live() method is no longer recommended since later versions of jQuery offer better methods that do not have its drawbacks.

Source:

1

Please notice: Your example is not from W3C (see title given by you). You cited w3schools and checking with other issues at stackoverflow you'll find out that there is no affiliation between w3c and w3school (except the first two characters).

3

also make sure that you look in w3schools browser supports to see if it will work

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like