715 posts
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Located in Belgium
+1 more
Hy-Studio
says

Hello,

I have a string

<p attribute="testAttribute">Some text</p>

I would like to “extract” the attribute value of the paragraph “tag” for further processing. But I don’t know if it’s possible in Javascript to get the value since the content is plain string.

Anyone know how to do this?

Thanks!

512 posts Abid Omar
  • Has been part of the Envato Community for over 6 years
  • Has referred 100+ members
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has been a beta tester for an Envato feature
+7 more
omarabid
says

If you use jQuery

var str = '<p attribute="testAttribute">Some text</p>';
var my_obj = $(str);
console.info(my_obj.attr('attribute')); // => testAttribute

But don’t use jQuery just to extract the attribute, an alternative is to do it manually (insert the string to the DOM ), and use an identifier for it, but I’m lazy to do that :p

2229 posts
  • Located in Europe
  • Made it to the Authors' Hall of Fame
  • Had an item featured on Envato Market
  • Participated in a focus group or interview to improve the user experience on Envato Market
+11 more
revaxarts
says

Or with regex:

var str = '<p attribute="testAttribute">Some text</p>';
alert(str.match(/<p attribute="(\w+)">(.*)<\/p>/)[1]);</p>
715 posts
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Located in Belgium
+1 more
Hy-Studio
says

I’m no expert in regular expressions, I find it hard to manage it :/. I’ll try the way that omarabid just proposed.

Thanks!

by
by
by
by
by
by