Reputation: 183
I want to pop over different forms on different button click. I'm using this code for that. but all button click shows first content form. How it solve? Code
<a href="#" class="button">Click 1</a>
<div id="modal">
<div id="heading">Content form 1</div>
</div>
<a href="#" class="button">Click 2</a>
<div id="modal">
<div id="heading">Content form 2</div>
</div>
<!--jQuery-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="js/jquery.reveal.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('.button').click(function (e) { // Button which will activate our modal
$('#modal').reveal({ // The item which will be opened with reveal
animation: 'fade', // fade, fadeAndPop, none
animationspeed: 600, // how fast animtions are
closeonbackgroundclick: true, // if you click background will modal close?
dismissmodalclass: 'close' // the class of a button or element that will close an open modal
});
return false;
});
});
</script>
Upvotes: 2
Views: 170
Reputation:
The correct attribute to use when identifying a group of elements is class
, not id
, which must be unique to an element. Applying class names instead of IDs, your markup would look like:
<div class="modal">
...
</div>
Since you are trying to manipulate the modal that precedes the clicked button, the best approach is to use the prev
method to select the element immediately preceding the button.
$('.button').click(function (e) {
$(this).prev('.modal').reveal({
...
});
return false;
});
Upvotes: 0
Reputation: 18591
As indicated, you have a non-unique ID in your divs, so $('#modal')
will only select the first one.
However, this will select all of them:
$("div[id=modal]")
See the following question in this regard: How to select all elements with a particular ID in jQuery?
Upvotes: 0
Reputation: 1619
You are using a non-unique id. It will only ever give you the first one.
<a href="#" class="button" data-modal="1">Click 1</a>
<div id="modal">
<div id="heading">Content form 1</div>
</div>
<a href="#" class="button" data-modal="2">Click 2</a>
<div id="modal">
<div id="heading">Content form 2</div>
</div>
<!--jQuery-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="js/jquery.reveal.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('.button').click(function (e) { // Button which will activate our modal
$('#modal' + $(this).data('id')).reveal({ // The item which will be opened with reveal
animation: 'fade', // fade, fadeAndPop, none
animationspeed: 600, // how fast animtions are
closeonbackgroundclick: true, // if you click background will modal close?
dismissmodalclass: 'close' // the class of a button or element that will close an open modal
});
return false;
});
});
</script>
Observer how:
The advantage of this method is that the links can be moved to wherever they need to be without needing to change the internals of the click event. Aka we do not need to do DOM searching via .next()
, .parent()
, .find()
etc.
Upvotes: 1