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 div
s 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.
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.
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.
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.
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>
HTML
<div id="replaceMe">i need to be replaced</div> <div id="iamReplacement">i am replacement</div>
JavaScript
jQuery('#replaceMe').replaceWith(jQuery('#iamReplacement'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With