I created a button so when I click on it a random card appears. On the random card(s) is an "x". I can't position the "x" in the top right corner and I don't know why it does not work.
I want to create a function so that when I click on the "x" the random card gets deleted.
Here is my HTML:
<button class="plus">
<div class="item">
<p> + </p>
</div>
</button>
<div id="newCardHolder">
</div>
<div id="cardPrototype" class="card" style="display:none;">
<p class="delete">x</p>
<span> Title </span>
<form name="theform" style="display:none;">
<input class="input-feld" type="text">
<br>
<input class="input-feld " type="text">
<br>
<input class="speichern"type="button" onClick="new Person()" value="Speichern">
<input class="abbrechen"type="button" onClick="new Person()" value="Abbrechen">
</form>
</div>
My CSS:
.input-feld {
font-family: TheSans Swisscom;
margin:3px;
}
.card {
width:300px;
margin-right:5px;
margin-left:5px;
margin-bottom:10px;
float: left;
padding:10px;
background-color: #BBBBBB;
border: 1px solid #ccc;
border-radius:10px;
}
.delete {
font-family:'TheSans Swisscom';
right:0;
top:0;
}
.speichern{
font-family:'TheSans Swisscom';
}
.abbrechen{
font-family:"TheSans Swisscom";
background-color: greenyellow;
}
And my jQuery:
$(document).ready(function () {
$("button.plus").on("click", function () {
var newCard = $('#cardPrototype').clone(true);
$(newCard).css('display', 'block').removeAttr('id');
$('#newCardHolder').append(newCard);
});
$('body').on('click', '.card', function () {
$(this).find('form').show();
$(this).find('span').remove();
});
});
To position your little x
with that CSS you'll need to set it to be relative to its parent, which is a little counter-intuitive if you haven't done it before.
.card {
position: relative;
}
.card .delete {
position: absolute;
}
To get it to act as a close button, you'll just need to duplicate the jQuery you have but do the opposite:
$('body').on('click', '.card .delete', function () {
$(this).closest('.card').remove();
});
That's one way of doing it, anyway.
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