Metaphysiker Metaphysiker - 2 months ago 10
jQuery Question

Pass value of button to append, jquery, Ruby on Rails

In my Ruby on Rails project, I want to pass via a button the content of that button to a list.

This is my script:

<script>
$(document).ready(function(){
$(document).on('click', ".btn2", function () {
$("ol").append("<li>Appended item</li>"); #<-- Problem here
});
});
</script>


And this is my button:

<button class="btn2"><%= date %></button>


When I press the button "btn2", another
<li>Appended item</li>
gets listed to the ol-element. Now, I want that, instead of "Appended Item", the <%= date %>-value should be listed. For example:

<button class="btn2">10-11-2014</button>


Should add "10-11-2014" to the list.

How do I accomplish this? Thanks in advance!

Answer

You can do it like this:

$(document).ready(function(){
  $(document).on('click', ".btn2", function () {
    var buttonContent = $(this).html();
    $("ol").append("<li>" + buttonContent + "</li>");
  });
});
Comments