2

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!

4 Answers 4

2
$('p > label + input').val(function() { return $(this).prev().text(); });

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

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

1 Comment

Wow. Thanks. 3 more chars to go.
2

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

$("p input").val(function() { return $(this).prev().text(); });

Comments

2
$('p').each(function() {
    $(this).children('input').val($(this).children('label').text());
});

http://jsfiddle.net/Fveph/

Comments

0

$("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?

3 Comments

This is a login form. I want to keep the labels, but display them as input values.
I'm okay with pulling the labels; I'm having some difficulties with creating a proper loop...
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.