0

I have this structure on form,

<input type="test" value="" id="username" />

<span class="input-value">John Smith</span>

<a href="#" class="fill-input">Fill Input</a>

when user click on the Fill Input ,

the data from span which has class input-value will be added to value, after clicking a tag the code should be look like this,

 <input type="test" value="john Smith" id="username" />

    <span class="input-value">John Smith</span>

    <a href="#" class="fill-input">Fill Input</a>

there are many forms element/input on the single page.

4 Answers 4

1

You can do it like this:

$('a.fill-input').click(function() {
   $(this).prevAll("input:first").val($(this).prev(".input-value").text());
});

Instead of looking only for the classes, this looks for the span and input just before the button you clicked...this means it works no matter how many of these you have on the page.

Unlike your example though, the value will be John Smith, with the same casing as it has in the span, if you actually want lower case, change .text() to .text().toLowerCase().

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

Comments

0
$('a.fill-input').click(function() {
    $('#username').val($('.input-value.').text());
});

2 Comments

Need to be said that this is jQuery code. Get jQuery at jquery.com. Get started at docs.jquery.com/Downloading_jQuery. jQuery documentation is at api.jquery.com. EDIT: Since this was tagged jQuery the author is likely to know jQuery, but i'm letting the information persist.
The question is tagged as jQuery. Pointless comment.
0

This should suffice for all inputs that you have so structured. It doesn't depend on the names of the elements. Note that prevAll returns the elements in reverse order so you need to look for the first input, not the last in the preceding inputs.

$('.fill-input').click( function() {
    var $input = $(this).prevAll('input[type=test]:first');
    var $value = $(this).prev('span.input-value');
    $input.val($value.text());
});

Comments

0

try:

$('a.fill-input').click(function(e){
  $('#username').val($('span.input-value').text());
   e.preventDefault();
   return false;
});

Comments

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.