Clément Andraud
Clément Andraud

Reputation: 9279

Retrieve elements in string with jQuery

I have a string like that :

<fieldset>
<legend>Sondage</legend>
<input type="hidden" value="formulaire_test/poll1352736068616/testencode" name="pollpost">
<p class="question">Q1</p>
<p class="response">
<input id="R010101" type="radio" value="A" name="R0101">
<label for="R010101">R11</label>
</p>
<p class="response">
<input id="R010102" type="radio" value="B" name="R0101">
<label for="R010102">r12</label>
</p>
<p class="question">Q2</p>
<p class="response">
<input id="R010201" type="radio" value="A" name="R0102">
<label for="R010201">r2</label>
</p>
<p class="response">
<input id="R010202" type="radio" value="B" name="R0102">
<label for="R010202">r22</label>
</p>
<p class="submit">
<input type="submit" value="Votez">
</p>
</fieldset>

I want with jQuery retrieve for example value of <legend>, values of <p class=question> or all inputs values..

Do you have any idea ?

I can put this string into a jQuery object $(mystring) and after ?

Thanks !

Upvotes: 0

Views: 64

Answers (3)

Sushanth --
Sushanth --

Reputation: 55750

just do

$('legend').html(); // Text inside Legend

var questions = new Array();
$('p.question').each(function(){
     questions.push( $(this).html() ) ; // Questions class text into array
});

var inputs = new Array();
$('input').each(function(){
     inputs.push(this.value) ; // Input values into array
});

If you want to find these inside $(mystring) just prepend the selector with

$('mystring').find( selector )

Upvotes: 0

nix
nix

Reputation: 3302

You can use

$(mystring).find('legend').text();

or

$(mystring).find('input').val();

Upvotes: 1

halilb
halilb

Reputation: 4115

You can use following code to push input values into an array:

var arr = new Array();
$(mystring).find('input').each(function() {
    arr.push($(this).val());
});

Upvotes: 2

Related Questions