allegutta
allegutta

Reputation: 5644

Change content with jquery?

I want to update the content in a span tag when a user click on a button. The span tag has a class called fa_button_id_counter and a id with the post id. It should be simple, but I can't make it work. Any suggestions?

view file:

<a class="like_button fa_button active" href="#<%= p.id %>" id="<%= p.id %>">FA</a>
<span class="fa_button_id_counter" id="<%= p.id %>">+<%= p.likes.count %></span>

.js file:

$ ->
  $(".like_button").click (e) ->
    e.preventDefault();
    id = $(this).attr('href')
    $(".fa_button_id_counter" + id).html "change to this"

Upvotes: -1

Views: 60

Answers (1)

000
000

Reputation: 27247

ids can't start with numbers. Gotta start with a-zA-Z.

To answer the question:

$(".fa_button_id_counter" + id).html('+'+(parseInt($(".fa_button_id_counter" + id).html(),10)+1))

parseInt takes a string and parses out a number. If "+8" is given, the number 8 will come out. However, you need to be careful to always provide the second parameter, base, set to 10. Otherwise javascript will try to guess the base for you. If the input is 015, the output will be 13, because it is guessing that your number is in base 8.

edit: Thanks mu is too short and vol7ron: Numeric ids are allowed if you change your doctype to <!DOCTYPE html>.

Upvotes: 1

Related Questions