0

I have a string like follows in javascript

var str='<img class="avatar  avatar-small 012345" src="/img/staff_avatar_profile.jpg" />&nbsp; olah blah';

I need to get every time the number like for this example 012345. Which will be ofcourse be different in different scenario and I also need to get the text olah blah which will be also different in different scenario.

I have the above string in a variable. ;)

How can i perform this in a efficient manner in java-script

8
  • What have you tried? Commented Sep 13, 2013 at 14:14
  • 1
    I assume you mean the numeric class? Commented Sep 13, 2013 at 14:15
  • I know but I gave that in a variable string Commented Sep 13, 2013 at 14:15
  • So that is stored in a variable? That's very relevant info! Commented Sep 13, 2013 at 14:15
  • Oh thank u ...i added in my question Commented Sep 13, 2013 at 14:16

4 Answers 4

2

The best way to parse HTML in a browser is to let the browser do it for you:

var div = document.createElement('div');
div.innerHTML = str;
var numbers = div.getElementsByTagName('img')[0].className.match(/ (\d+)/)[1];

Here's the fiddle: http://jsfiddle.net/CGuLC/1/

Sign up to request clarification or add additional context in comments.

2 Comments

Its nice but very fragile if i have other numbers also sometimes in my string
@SaurabhKumar - the regex will only match the first set of number it can find.
0

You could use jQuery if it works there to get an only numeric class name. Take a look at jQuery - selectors on a html string to get a clue.

2 Comments

I like this the best. When I do $(myString).find('.user').text()..it returns me nothing....
@SaurabhKumar, then please check this answer as useful :)
0

Use the data attribute instead of class.

<img class="avatar  avatar-small" data-id="012345" src="/img/staff_avatar_profile.jpg" />

Then use jQuery to get the data attribute.

jQuery('.avatar').data('id');

Sources: http://html5doctor.com/html5-custom-data-attributes/ http://api.jquery.com/data/

6 Comments

he has string not html image
And also didn't mention jQuery anywhere
It doesn't matter if its a string or an element. He can still create a html element in jQuery and then access the data attribute that way.
@CharliePrynn yes ,that's the reason why i told him this , because i was trying to convert string to dom element.The difference between me and you is that i tried to parse string without div , can you tell me why ?
I'm not sure I undertand what you mean @FaceOfJock.
|
0

If you need to store data for an element, one good way to achieve that is to use the data-key attribute with jQuery:

$('.avatar').data('number', '012345'); // set value
var number = $('.avatar').data('number'); // get value

The rendered markup will look like this:

<img class="avatar  avatar-small" data-number="012345" src="/img/staff_avatar_profile.jpg" />

Comments

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.