MeltingDog
MeltingDog

Reputation: 15404

Get element from string

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?

Upvotes: 0

Views: 710

Answers (3)

matt3141
matt3141

Reputation: 4423

Get the hidden input like so:

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

Upvotes: 1

Ry-
Ry-

Reputation: 224859

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.

Upvotes: 1

bretterer
bretterer

Reputation: 5781

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

Upvotes: 0

Related Questions