Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load the content from another file clicking on a link?

Tags:

html

jquery

I have this code to load the content from another file

$(document).ready(function(){
  $("button").click(function(){
    $("#divA").load("book.html #divB");
  });
});
</script>

<button>Get</button>

This works, but I need the same thing with click on a link, instead of button

<a href="book.html">Get</a>

As per the user comment

I have another links on page. Only specific link should load the content.

like image 491
Alegro Avatar asked Dec 17 '25 07:12

Alegro


2 Answers

Use the anchor tag as selector and make sure you stop the default action.

$(document).ready(function(e){
  $("a#id").on('click',function(){  // $("a#id")  or $("a.class") to target 
                                    //  specific anchors
    e.preventDefault();
    $("#divA").load("book.html #divB");
  });
});
like image 115
Sushanth -- Avatar answered Dec 19 '25 20:12

Sushanth --


Simply use a instead of button

$(document).ready(function(){
  $("a").click(function(e){
    e.preventDefault();
    $("#divA").load("book.html #divB");
  });
});

For specific a try this

$(document).ready(function(){
  $("a#pass_your_id_here").click(function(e){
    e.preventDefault();
    $("#divA").load("book.html #divB");
  });
});
like image 26
Mr. Alien Avatar answered Dec 19 '25 22:12

Mr. Alien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!