1

IN a JavaScript Event I only have access to a string in this format below...

var innerHTML = '<img src="https://avatars3.githubusercontent.com/u/7988569?v=3&amp;s=40"
class="item-image picker-item-thumb"><i id="2">Item 2</i>';

I need to extract the ID value from this part <i id="2"> so I would have 2 in a variable.

Also none of the other HTML tags will ever have an ID besides the <i>

I'm not sure how to do this?

11
  • 1
    Parse the HTML to DOM elements, find the i element and access its id property. Commented Oct 29, 2015 at 0:15
  • 1
    Convert it to DOM elements by assigning it to the innerHTML of a DocumentFragment. Then use DOM methods to access the elements and attributes. Commented Oct 29, 2015 at 0:15
  • 1
    It's even easier if you use jQuery: $("<div>", {html: innerHTML}).find("i").attr("id") Commented Oct 29, 2015 at 0:16
  • Why is your HTML a String in the first place? It's just Element.id in JavaScript if you're working on the DOM. Commented Oct 29, 2015 at 0:17
  • 1
    if you are using jQuery can use $(innerHTML).filter('i')[0].id; Commented Oct 29, 2015 at 0:32

2 Answers 2

2

Use regex capturing group.

If the id is always numeric, use:

/<i id="(\d+)"/.exec(innerHTML)[1]

Else, you can use

/<i id="([^"]+)"/.exec(innerHTML)[1]

Suggest to look at the comments for further information!

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

8 Comments

That would return foo" bar="baz for <i id="foo" bar"baz" />.
@FelixKling Well the question is specifically for the format <i id="2">, but yeah. Need to keep in mind.
@aarjithn 1. Fix InnerHTML to innerHTML. Better use /<i id="(\d+)"/.exec(innerHTML)[1] quite fine for numerical ID
@aarjithn this is the issue Felix pointed out - jsbin.com/lodebu/2/edit?html,css,js,console,output - where (\d+) prevents it. Though worth noting that Error will be thrown using your examples if an Alpha character is used as ID.
@aarjithn: Simpler would be [^"]+ :)
|
0

You can use regex to do it:

(?<=i id=").+(?=")

As pointed by felix king you can try the following as javascript doesn't support lookbehinds:

/i id="(.+)"/

var innerHTML = '<img src="https://avatars3.githubusercontent.com/u/7988569?v=3&amp;s=40"
class="item-image picker-item-thumb"><i id="2">Item 2</i>';
var arr = innerHTML.match(/i id="(.+)"/)[1];
alert(arr);

arr will have all the matches.

4 Comments

JavaScript doesn't support lookbehinds
Uncaught SyntaxError: Invalid regular expression: /(?<=i id=").+(?=")/: Invalid group(…).
@NishanthMatha (.+)" might fail if user adds another attribute to the <i> element - since .+ will match any character > since the last occurrence of ". (\S+) is a somewhat better solution
Now it's the same solution as aarjithn's with the same initial issues. Have a look at the comments on that answer.

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.