sm21guy
sm21guy

Reputation: 626

get the number from a line of html code using jquery/javascript

how can i get the number inside a html code.

For example:

<a href="#">Comments</a>(10)

How can i get the 10 from the line of code above.

This includes removing the link html , comment word , plus the brackets around the number "10"

How can do this using jquery/javascript?

Upvotes: 1

Views: 365

Answers (2)

Georgi Naumov
Georgi Naumov

Reputation: 4201

You can try something like this: http://pastebin.com/PV9f0PQH

I paste example in pastebin because here is problems when paste part of html tags.

Upvotes: 0

Esailija
Esailija

Reputation: 140220

Given html

<a id="#element" href="#">Comments</a>(10)

jQuery:

var value = $("#element").get(0).nextSibling.nodeValue; //"(10)"
value = parseFloat( value.substr(1) ); //10

If you have a string ('<a href="#">Comments</a>(10)'), then

var value = $('<div>', {html: '<a href="#">Comments</a>(10)'}).contents().get(1).nodeValue; //"(10)"
value = parseFloat( value.substr(1) ); //10

Upvotes: 4

Related Questions