rozero
rozero

Reputation: 149

How to get value from a tag

I have a link like this

<a id="idPage" href="#">${currentPage}</a>

and in this case I can get value from tag doing this way

$(this).text();

now let's the link will be like this

<a id="idPage" href="#">next page</a> 
<!-- the value of ${currentPage} still must to be available for a script-->

How to pass the value and how to get in script in this case?

Upvotes: 0

Views: 257

Answers (2)

CollinD
CollinD

Reputation: 7573

An appropriate way to do this would be to use a data attribute and jQuery's $.data method like so.

<a id="idPage" href="#" data-currentpage="${currentPage}">next page</a>

And then to access that value,

$("#idPage").data('currentpage')

Upvotes: 1

Richard Hamilton
Richard Hamilton

Reputation: 26434

The simple solution is to store the value in a variable

jQuery

var tagValue = $("#idPage").text();

JavaScript

var tagValue = document.getElementById("idPage").innerHTML;

but it's probably the best idea to use HTML5 data attributes as mentioned in the comments.

Upvotes: 0

Related Questions