2

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?

Linas
  • 4,380
  • 17
  • 69
  • 117

3 Answers3

5

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();

Demo

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
1

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 '.

Community
  • 1
  • 1
gdoron
  • 147,333
  • 58
  • 291
  • 367
0

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();
Naftali
  • 144,921
  • 39
  • 244
  • 303