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!

486 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

2122 posts
  • 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
  • Interviewed on an Envato blog
+9 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!

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by