Metaphysiker Metaphysiker - 1 year ago 58
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:

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

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 Source

You can do it like this:

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