Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Div with another Div

Tags:

html

jquery

swap

I have this thing I m trying to do. I have a main picture of a map and within that map there are regions. These regions have hot spots on them so you can click them and it will replace the whole map with only the region. (just a simple div swap).

I need it as a div because in this div i have the hot spots listed.

There are a total of 4 divs that I am going to need to do this with.

If anyone could help me that would be awesome!

So links that are listed in a table need to replace the image in a separate div.

<tr class="thumb"></tr> <td>All Regions (shows main map) (link)</td> </tr>  <tr class="thumb"></tr> <td>Northern Region (link)</td> </tr>  <tr class="thumb"></tr> <td>Southern Region (link)</td> </tr>  <tr class="thumb"></tr> <td>Eastern Region (link)</td> </tr> </table>   <div>All Regions image</div> <div>northern image</div> <div>southern image</div> <div>Eastern image</div> 

I am not allowed to post images because I don't have enough points so I know the image links wont work.

like image 492
Jake Avatar asked Feb 21 '11 06:02

Jake


People also ask

How to replace a div in jQuery?

To replace a DOM element with the specified HTML or DOM elements using jQuery, use the replaceWith() method. The replaceWith (content) method replaces all matched elements with the specified HTML or DOM elements. This returns the JQuery element that was just replaced, which has been removed from the DOM.

How do I toggle between divs?

To toggle a div visibility in jQuery, use the toggle() method. It checks the div element for visibility i.e. the show() method if div is hidden. And hide() id the div element is visible. This eventually creates a toggle effect.

How to replace innerHTML?

To set the value of innerHTML property, you use this syntax: element. innerHTML = newHTML; The setting will replace the existing content of an element with the new content.


2 Answers

You can use .replaceWith()

$(function() {      $(".region").click(function(e) {      e.preventDefault();      var content = $(this).html();      $('#map').replaceWith('<div class="region">' + content + '</div>');    });    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    <div id="map">    <div class="region"><a href="link1">region1</a></div>    <div class="region"><a href="link2">region2</a></div>    <div class="region"><a href="link3">region3</a></div>  </div>
like image 110
Gowri Avatar answered Sep 24 '22 05:09

Gowri


HTML

<div id="replaceMe">i need to be replaced</div> <div id="iamReplacement">i am replacement</div> 

JavaScript

jQuery('#replaceMe').replaceWith(jQuery('#iamReplacement')); 
like image 27
Praveen Prasad Avatar answered Sep 23 '22 05:09

Praveen Prasad