Reputation: 169
I am trying to display one specified div when I am clicking on button with class .showFormButton, but unfortunetly every div with class .showForm appears. How to make only one .showForm div appear using jQuery? JQUERY:
$(document).ready(function() {
$(".showFormButton").click(function(e){
e.preventDefault();
$(".showForm").css('display', 'block');
$(".showForm").css('position', 'absolute');
});
});
HTML
print "<input type='submit' class='showFormButton' VALUE='Ulubiony' onclick='ShowDiv()'>";
print "<div style='display: none; margin-left: -170px; margin-top: -60px; background-image: linear-gradient(rgb(15, 159, 220),rgb(14, 108, 148));' class='showForm'>";
print "<div >";
print "<p><b>Dodaj do ulubionych pracownika</b></br> [".$row['imie1']. " ".$row['nazwisko1']."]</p>";
print "<p>Osobista notatka</p>";
print "<FORM ACTION='view_tm.php' METHOD='POST' ENCRYPT='multipart/form-data' name='ulubiony_pracownik_dodaj'>";
print "<textarea name='id_pracownik_op_notatka' rows='10' cols='45'>". $row['notatka'] ."</textarea>";
print "<input type='hidden' name='id_pracownik_op' value='" . $row['id_pracownik'] . "'>";
print "<INPUT TYPE='submit' id='showFormButton' NAME='ulubiony_pracownik' VALUE='Dodaj do ulubionych'>";
print "</form>";
print "</div>";
Upvotes: 0
Views: 78
Reputation: 1432
You can distinguish the div by using an attribute
$(document).ready(function() {
$(".showFormButton").click(function(e){
e.preventDefault();
$(".showForm[showme='1']").css('display', 'block');
$(".showForm[showme='1']").css('position', 'absolute');
});
});
HTML
print "<input type='submit' class='showFormButton' VALUE='Ulubiony' onclick='ShowDiv()'>";
print "<div showme='1' style='display: none; margin-left: -170px; margin-top: -60px; background-image: linear-gradient(rgb(15, 159, 220),rgb(14, 108, 148));' class='showForm'>";
print "<div >";
print "<p><b>Dodaj do ulubionych pracownika</b></br> [".$row['imie1']. " ".$row['nazwisko1']."]</p>";
print "<p>Osobista notatka</p>";
print "<FORM ACTION='view_tm.php' METHOD='POST' ENCRYPT='multipart/form-data' name='ulubiony_pracownik_dodaj'>";
print "<textarea name='id_pracownik_op_notatka' rows='10' cols='45'>". $row['notatka'] ."</textarea>";
print "<input type='hidden' name='id_pracownik_op' value='" . $row['id_pracownik'] . "'>";
print "<INPUT TYPE='submit' id='showFormButton' NAME='ulubiony_pracownik' VALUE='Dodaj do ulubionych'>";
print "</form>";
print "</div>";
Upvotes: 0
Reputation: 67207
You have to use this
object to target your selection over the clicked element,
$(document).ready(function() {
$(".showFormButton").click(function(e){
e.preventDefault();
var elm = $(".showForm", this)
elm.css({'display' : 'block', 'position' : 'absolute' });
});
});
Instead of using .css()
for setting the properties, you could use .addClass()
. Because it would be easy in maintaining, like removing the added properties in future.
$(document).ready(function() {
$(".showFormButton").click(function(e){
e.preventDefault();
var elm = $(".showForm", this)
elm.addClass("clicked");
});
});
CSS:
.clicked { display:block; position:absolute; }
Edit:
Since the target element is the next sibling the the current element, you have to use .next(selector)
at this context.
$(document).ready(function() {
$(".showFormButton").click(function(e){
e.preventDefault();
var elm = $(this).next(".showForm")
elm.css({'display' : 'block', 'position' : 'absolute' });
});
});
Upvotes: 1
Reputation: 9113
jQuery has the .show()
function to make hidden stuff visible. Take a look at this code snippet:
$(document).ready(function () {
$(document).on('click', '.action-btn', function () {
$('.hidden-div').show();
});
));
<div style="display: none;" class="hidden-div">
Hidden text
</div>
<button class="action-btn">Show hidden div</button>
Upvotes: 0