FOOD

Ginger Strawberry Spritzer

javascript html jquery

De ce funcția .data() din jQuery nu merge?

Am probleme cu funcția .data(), aceasta este deja a doua oară. Dacă folosesc .data - nu se adaugă nici un atribut:

delete_button.data("to-do", toDoCount);

Mai înainte foloseam funcția attr, și ea funcționează perfect

delete_button.attr('data-to-do', toDoCount);

Am făcut totul ca în documentația jQuery. $("body").data("foo", 52);

// Create an initial toDoCount variable
    var toDoCount = 0;

    //  On Click event associated with the add-to-do function
    $("#add-to-do").on("click", function(event) {
      // prevent form from submitting
      event.preventDefault();

      // Get the to-do "value" from the textbox and store it a variable
      var thing_todo = $('#to-do').val().trim();

      // Create a new variable that will hold a "<p>" tag.
      // Then give it an ID in the following form:
      // "item-4" or "item-3" or "item-99", where the number is equal to toDoCount.
      // Then append the to-do "value" as text to this <p> element.
      var new_todo_element = $("<p>");
      new_todo_element.attr('id', 'item-' + toDoCount);
      new_todo_element.append(' ' + thing_todo);

      // Create a button with unique identifiers based on what number it is in the list. Again use jQuery to do this.
      // Give your button a data attribute called data-to-do and a class called "checkbox".
      // Lastly append the letter X inside.
      var delete_button = $("<button>");
      delete_button.data('to-do', toDoCount);
    //   delete_button.attr('data-to-do', toDoCount);
      delete_button.addClass('checkbox');
      delete_button.append('X');

      // Append the button to the to do item
      new_todo_element.prepend(delete_button);

      // Add the button and toDoItem to the to-dos div
      $('#to-dos').append(new_todo_element);

      // Clear the textbox when done
      $('#to-do').val('');

      // Add to the toDoCount
      toDoCount += 1;

    });

    //  When a user clicks a check box then delete the specific content
    //  (NOTE: Pay attention to the unusual syntax here for the click event.
    //  Because we are creating click events on "dynamic" content, we can't just use the usual "on" "click" syntax.)
    $(document.body).on("click", ".checkbox", function() {

      // Get the number of the button from its data attribute and hold in a variable called  toDoNumber.
    //   var item_id =

      // Select and Empty the specific <p> element that previously held the to do item number.

    });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<head>

  <meta charset="UTF-8">
  <title>To Do App!</title>

  <style>
    #todo-item {
      font-weight: 700;
      font-size: 2em;
    }
  </style>
</head>

<body>

  <!-- Title -->
  <h1>My To Do's!</h1>

  <!-- Input Form -->
  <form>
    <span id="todo-item">To Do Item: <input id="to-do" type="text" ><input id="add-to-do" value="Add Item" type="submit"></span>
  </form>

  <hr>

  <!-- To-Do List -->
  <div id="to-dos"></div>

Cum pot seta atributele acestui buton prin funcția data? Mulțumesc

0

Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Fusce vulputate eleifend sapien. Vestibulum purus quam, scelerisque ut, mollis sed, nonummy id, metus. Nullam accumsan lorem in dui. Cras ultricies mi eu turpis hendrerit fringilla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere.

Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum. Sed aliquam ultrices mauris. Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Praesent adipiscing. Phasellus ullamcorper ipsum rutrum nunc. Nunc nonummy metus. Vestibulum volutpat pretium libero.

Ingredients

SERVINGS 4
  • 2 bunches of strawberries
  • 1 lemon
  • 1 bunch of mint
  • 1 piece of ginger chopped
  • 1 bottle of sparkling water

Instructions

  1. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum.
  2. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus.Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo.
  3. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum.
  4. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue.
  5. Duis leo. Sed fringilla mauris sit 
amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.

Categories