To get value of element by id from document is very easy:
var example = document.getElementById('myId');
But what if I would like to get value by id from variable:
var example1 = '<a href="example.html" id="myId">lorem ipsum</a>'
Is there any way?
| SitePoint Sponsor |
To get value of element by id from document is very easy:
var example = document.getElementById('myId');
But what if I would like to get value by id from variable:
var example1 = '<a href="example.html" id="myId">lorem ipsum</a>'
Is there any way?

Do you mean for instance the lorem ipsum text?
Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
Having lame problems with your code? Let us help by using a jsFiddle
no and I am sorry, I didn't give good example. This one is better:
So how to get element by id from this variable to alert "<a href="example.html" id="myId">lorem ipsum</a>"HTML Code:var example1 ='<div><span>sdf</span><a href="example.html" id="myId">lorem ipsum</a></div>'

A regular expression would be needed to grab the id of the element, I'm not too good with them but there may be another user here that knows a pre-designed expression for this type of search.
Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
Having lame problems with your code? Let us help by using a jsFiddle
Bookmarks