Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace one div with another when clicking the html hyperlink [duplicate]

Possible Duplicate:
Replace Div with another Div

Hi,

I have got 2 navigation menus, what I want to do when a client click on a link of first menu , the div id="img" should be replaced by div id="img2" , similarly when i click on the link of menu 2 div id="img2" should be replaced by div id="img" . Any ideas or suggestions ..

like image 663
Mr A Avatar asked Jan 30 '26 23:01

Mr A


1 Answers

Something like this might help you:

//Using jQuery Change div attribute "id" on click:

$('#link_1_id').click(function(){ 
      $('#img').attr('id','img2'); 
});

Edit: Oops didnt understand the question. To replace a div with another when clicking a link element:

HTML:
<a href="#" id="link_1">Press me</a>
<a href="#" id="link_2">Press me</a>

<div id="div_1"> Content1 </div>
<div id="div_2"> Content2 </div>

Jquery:
$(document).ready(function(){

  // Hide div 2 by default
  $('#div_2').hide();

  $('#link_2').click(function(){ 
      $('#div_1').hide();
      $('#div_2').show();
  });

  $('#link_1').click(function(){ 
      $('#div_2').hide();
      $('#div_1').show();
  }); 
});

To add sliding effects take a look at .slideDown() or slideUp(). http://api.jquery.com/slideDown/

like image 134
Limpan Avatar answered Feb 01 '26 15:02

Limpan



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!