Before asking this, I have checked here
I am very new to php
and JavaScript
. But the problem here is I echo
results from my database with divs
example below.
echo "<div class="togBtn"><div class="result">$results</div></div>";
And the html
output is something like
<div class="togBtn"><div class="result">firstresult</div></div>
<div class="togBtn"><div class="result">secondresult</div></div>
But i then wrote a JavaScript
that toggles .result
when .togBtn
is click
$(document).ready(function() {
$(".togBtn").click(function() {
$(".result").slideToggle();
});
});
But the problem is that when i click
the .togBtn
, both the div
with firstresult and secondresult is toggled
. My question is, is it possible that when i click
.togBtn
only that div
results is toggled rather than both echoed
results. Am echoing the results wrongly such that it causes the problem. Any suggestion would do. Thanks in advance.
The answer was to use the jQuery .next() .prev()
with $(this)
$('.togBtn').click(function() {
$(this).next().slideToggle();
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.