2

I have atleast 20 links in a page and on each link i have to show a popup with different content.Is there any way that i can call only one javascript function and pass it id of div that has to be displayed as popup.My html structure looks like this

<div id='1'>    
<a href='#'>Moreinfo</a>
<div id='popup1'>
Content for first link
</div>
</div>

<div id='2'>
<a href='#'>Moreinfo</a>
<div id='popup2'>
Content for first link
</div>
</div>

Also if i click other link first one should close. Please give suitable example in reply. Thanks

3
  • are popup divs hidden? Commented Apr 12, 2013 at 10:26
  • 1
    Give them all a class and make use of this Commented Apr 12, 2013 at 10:26
  • This link might help: jquerytools.org/demos/overlay/multiple.html Commented Apr 12, 2013 at 10:27

3 Answers 3

1

I would try something like this

<div class="popupHolder">
    <div class="clickToPopup">Click here</div>
    <div class="popup">your popup content</div>
</div>
<div class="popupHolder">
    <div class="clickToPopup">Click here</div>
    <div class="popup">your popup content</div>
</div>
<script type="text/javascript">
    $(document).ready(function() {
        $(".clickToPopup").click(function(){
             $(".popup").hide();
             $(this).parent("div.popupHolder").children("div.popup").show();
        });
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

jsFiddle Demo

You could do it like this

$("div[id^=popup]").hide();
$("a").click(function(){
 $("div[id^=popup]").hide();
 var id = this.parentNode.id;
 $("#popup"+id).show();
});

Comments

0
<div id='1' class="div">
<a href='#'>Moreinfo</a>

    <div id='popup1' class="poppup">Content for first link</div>
</div>
<div id='2' class="div">
<a href='#'>Moreinfo</a>

    <div id='popup2' class="poppup">Content for first link</div>
</div>

JQUERY

 $('.div').click(function () {
        $('.poppup').hide();
        $(this).children('.poppup').show();
    });

JS FIDDLE LINK

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.