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!

484 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 100+ members
  • Has sold $40,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+6 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

2024 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $250,000+ on Envato Market
  • Sells items exclusively on Envato Market
  • Contributed a free file of the month
+8 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