Reputation: 4408
I need to find input value in a string, it should look something like this:
var someHtml = "some text, <div id='somediv'></div> <input type='text' value='somethin' />";
$(someHtml).find('input').val();
this ofcourse doesn't work so how can i do that?
Upvotes: 2
Views: 26769
Reputation: 150253
You mixed up the someHtml
string.
Use '
for strings inside "
strings...
var someHtml = "some text, <div id='somediv'></div> <input type='text' value='somethin' />";
$(someHtml).find('input').val();
See this question about when to use "
and when to use '
.
Upvotes: 1
Reputation: 69905
First you need to escape the double quotes or use single quotes in someHtml
string which I don't think so is the same in actual case but still. And instead of using find
you have to use siblings
because input
is the sibling
inside $(someHtml)
or wrap the whole html
inside a div
and then can use find
method.
Try this.
$(someHtml).siblings('input').val();
Alternatively you can use this too.
$(someHtml).wrap('<div />').parent().find('input').val();
Upvotes: 6
Reputation: 146310
You can see straight from the SO markup.
Escape your quotes!
var someHtml = "some text, <div id=\"somediv\"></div> <input type='text' value='somethin' />";
$(someHtml).find('input').val();
Upvotes: 0