0

I have a large string in a variable that includes a whole bunch of HTML tags.

I want to get the value of a hidden input field within the string and store it in its own var.

<input type="hidden" value="WantThis" />

Can anyone help me out at all?

3
  • can you post complete html tag? You can use Jquery to get the value! Commented Jun 25, 2012 at 2:40
  • 1
    I'm confused... so you have a var that is a large string with HTML tags, then you have a hidden input, and you want to store it in its own var? Wait.. what? Commented Jun 25, 2012 at 2:40
  • Explain more. A simple syntax like this $('input[type=hidden]').val() can achieve what you want, that is if I understand you correctly. Commented Jun 25, 2012 at 2:41

3 Answers 3

1

You can parse the HTML with jQuery to get the value:

var theValue = $(myString).find('input[name=something]').val();

I'm assuming the hidden field has a name. If it doesn't, you'll need to specify input[type=hidden] and find it using its position relative to the rest of the content.

If your string does not already have a root element and the <input> is not nested, you'll probably want to use $('<div>' + myString + '</div>') instead.

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

Comments

1

Get the hidden input like so:

$(html).find("input[type=hidden]").val()

Comments

0

Create an ID for the hidden input and call it like normal

<input type="hidden" value="WantThis" id="myInput" />

Then call it

var myval = $('#myInput').val();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.