最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How Do I pass a value in a input box, to another input box - Stack Overflow

programmeradmin0浏览0评论

I am trying to pass values between boxes.

So, When a User types inside of the first text box:

<input type="text" placeholder="Your personal message" id="valbox"></input>
<input type="submit" name="design1" id="butval" value="Choose Design"></input>

Then they click the 'choose design' button, and what they typed in, gets passed to another input text box on the same page.

this is the second input box i want to pass it to.

<input type="text" class="input-text" name="billing_last_name" id="billing_last_name" placeholder="" value="">

Any help would be much appreciated thank you

I am trying to pass values between boxes.

So, When a User types inside of the first text box:

<input type="text" placeholder="Your personal message" id="valbox"></input>
<input type="submit" name="design1" id="butval" value="Choose Design"></input>

Then they click the 'choose design' button, and what they typed in, gets passed to another input text box on the same page.

this is the second input box i want to pass it to.

<input type="text" class="input-text" name="billing_last_name" id="billing_last_name" placeholder="" value="">

Any help would be much appreciated thank you

Share Improve this question asked Sep 9, 2013 at 0:44 user2759977user2759977 271 gold badge2 silver badges8 bronze badges 1
  • so what have you done so far? – artsylar Commented Sep 9, 2013 at 1:00
Add a ment  | 

3 Answers 3

Reset to default 1

Live Demo

Instead of a submit type input use a button type input.

HTML

<input type="text" placeholder="Your personal message" id="valbox"></input>
<input type="button" name="design1" id="butval" value="Choose Design"></input>
<input type="text" class="input-text" name="billing_last_name" id="billing_last_name" placeholder="" value="">

JS

window.onload = function(){
    document.getElementById('butval').onclick = function(){
        document.getElementById('billing_last_name').value = document.getElementById('valbox').value;   
    }
}; 

First add a clicklistener for the submit button and inside that callback pass the text through the elements

document.getElementById("butval").addEventListener("click", function(event){
    var text = document.getElementById("valbox").value;
    document.getElementById("billing_last_name").value = text;
    event.preventDefault();
    return false;
});

this is by far easiest in jquery given

<input type="text" placeholder="Your personal message" id="valbox"></input>
<input type="submit" name="design1" id="butval" value="Choose Design"></input>

<input type="text" class="input-text" name="billing_last_name" id="billing_last_name" placeholder="" value="">

use a simple

$("#butval").click(function(event){
    $("#billing_last_name").html("<p>"+$("#valbox").html()+"</p>");
    event.preventDefault();
});

but better change type="submit" to type="button" then you can remove the essentially unnecessary line event.preventDefault();

发布评论

评论列表(0)

  1. 暂无评论