Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jQuery to do an onClick div refresh

This should work right? I have not a clue as to why it's not. I have to be doing something wrong.

<div id="randomdiv">text</div>
    <a id="refresh">click</a>

    <script>
    $(function() {
      $("#refresh").click(function() {
         $("#randomdiv").load("index.php")
      })
    })
    </script>
like image 833
homework Avatar asked Sep 09 '09 19:09

homework


People also ask

How do I reload a div without reloading the whole page?

Use this. $('#mydiv'). load(document. URL + ' #mydiv');

Can you give a div an onclick?

The answer is definitely yes, but you will need to write javascript code for it. We can use a click handler on the div element to make it clickable.

How do I stop jQuery from refreshing?

You can use event. preventDefault() to prevent the default event (click) from occurring.


1 Answers

What happen if you do this?

<a id="refresh" href="#">click</a>

<script>
    $(function() {
      $("#refresh").click(function(evt) {
         $("#randomdiv").load("index.php")
         evt.preventDefault();
      })
    })
</script>
like image 109
marcgg Avatar answered Oct 25 '22 06:10

marcgg