0

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 !

3 Answers 3

2

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());
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can use

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

or

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

3 Comments

Yes, and if i want retrieve "Q1" in "<p class="question">Q1</p>" ?
then you can get it this way: $(mystring).find('p.question').text();
but you have more than 1 p with question class in your string, so you may try to add .eq(0) to get the specific <p>.
0

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 )

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.