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?

2
  • Take a good look at the quotes in string your are trying to store in the variable Commented Jan 30, 2012 at 15:37
  • Take a look on this question. Commented Jan 30, 2012 at 15:41

3 Answers 3

6

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

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

Comments

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

Comments

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

Comments

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.