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,234
  • 16
  • 68
  • 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
  • 68,720
  • 11
  • 89
  • 123
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 is supporting Monica
  • 142,542
  • 55
  • 282
  • 355
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
  • 142,114
  • 39
  • 237
  • 299