Jquery clickable div

Click here

I want to make this div interactive and it should call the following function:

<script type="text/javascript">
$(document).ready(function(){
    $("#generate").click(function(){
        $("#quote p").load("script.php");
    });
});
</script>

      

how can i make this work?

+2


source to share


2 answers


Give an div

ID generate

or any other (of course sensible) ID to uniquely select it:

<div id="someid" class="clickme"> click here </div>

<script type="text/javascript">
$(document).ready(function(){
    $("#someid").click(function(){
        $("#quote p").load("script.php");
    });
});
</script>

      

Please note that the ID must be unique throughout the document.



You can also just use a class to select an element, but it will select any element of that class and bind a handler to all of them:

$(document).ready(function(){
    $(".clickme").click(function(){
        $("#quote p").load("script.php");
    });
});

      

Read the documentation about selectors , especially ID and class .

+5


source


Or specify <div>

a id

of generate

:

<div id="generate">

      

or replace #generate

with .clickme

in JS:



$('.clickme').click(function() {
    // ...
});

      

This is pretty trivial. To learn more about using jQuery, I highly recommend going through their tutorials . If you prefer a book, I recommend jQuery in action .

+3


source







All Articles