Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The positioning of a button does not work

Tags:

html

jquery

css

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();
    });
});
like image 927
Sandro21 Avatar asked Dec 19 '22 14:12

Sandro21


1 Answers

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.

like image 167
moopet Avatar answered Jan 08 '23 03:01

moopet