views:

30

answers:

4

I need a jQuery function that'll go through paragraphs with the following structure:

<p>
  <label>some label</label>
  <input type="text" value=""/>
</p>

The function should use the label text as input value.

Thanks!

A: 

$("p > label") To parse the structures, then you can use html() to get the value, but that returns the first node's value... What do you want to do with the label text?

Brian
This is a login form. I want to keep the labels, but display them as input values.
konzepz
I'm okay with pulling the labels; I'm having some difficulties with creating a proper loop...
konzepz
OK then using each to loop through the elements and access the first and last child respectively to take the values from one and set to the other, like in the other responses.
Brian
+2  A: 
$('p > label + input').val(function() { return $(this).prev().text(); });

Example: http://jsfiddle.net/D392c/

patrick dw
Wow. Thanks. 3 more chars to go.
konzepz
@konzepz - You're welcome. :o)
patrick dw
+2  A: 

You can do it using .val() with a function, like this:

$("p input").val(function() { return $(this).prev().text(); });
Nick Craver
+2  A: 
$('p').each(function() {
    $(this).children('input').val($(this).children('label').text());
});

http://jsfiddle.net/Fveph/

Mark