0

I have this div in my rails view which renders a collection of users:

<div id="users" data-current_page="<%[email protected]_page %>">
  <%= render @users %>
</div>

and this jquery function (used to load more users on click action)

$(document).ready(function() {
  var current_page = $("#users").attr("data-current_page")
  $("a.next").click(function() {
    var next_page = ++current_page
    $.get("/users.js?page=" + next_page)
    $("#users").attr("data-current_page").replaceWith(next_page) # this doesn't work
    return false;
  });
});

In order to increment $("#users").attr("data-current_page"), I tried this:

$("#users").attr("data-current_page").replaceWith(next_page)

That is to say if data-current_page is set to 1 on a click it will set to 2 etc... but it doesn't work. Is it possible to update this html5 attribute through jquery?

Thanks for any idea!

2 Answers 2

2

The proper syntax for changing an attribute in jQuery is

$('#users').attr('data-current_page', next_page);
Sign up to request clarification or add additional context in comments.

Comments

1

You should check jquery's docs, the syntax you are looking for to set an attribute is

$("your_selector").attr("attribute_name",new_value);

So in your case, it would be something like:

$("#users").attr("data-current_page",++current_page);

You should also be careful with your use of ++, as it seems you are incrementing it twice per click!

cheers

2 Comments

Thanks for your advice I am looking into this
You're welcome, my recommendation when working with JS, take a browser with a JS console and test your code in it (I really like chrome and it's developer tools).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.