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

javascript - Pass a value from button to input value - Stack Overflow

programmeradmin1浏览0评论

I am trying to pass a value from a button to an input value with Javascript. What i have right now is this

this inside a javascript file

var test2 = 5;
appendeddatahtml = '<div class="test1"><button id="btn" type="button" value="' + test2 + '">This is it!</button></div>';
$("#test1").append(appendeddatahtml);

And this is the form above the script

<input type="text" name="latlon" id="latlon" style="display: none; " value="" />

First i tried to make an alert with this code

$(document).ready(function() {
  $("#btn").click(function() {
    alert("The button was clicked.");
  });
});

But it didn't alerted anything. Is there any way to pass the value from the button to the input value?

I am trying to pass a value from a button to an input value with Javascript. What i have right now is this

this inside a javascript file

var test2 = 5;
appendeddatahtml = '<div class="test1"><button id="btn" type="button" value="' + test2 + '">This is it!</button></div>';
$("#test1").append(appendeddatahtml);

And this is the form above the script

<input type="text" name="latlon" id="latlon" style="display: none; " value="" />

First i tried to make an alert with this code

$(document).ready(function() {
  $("#btn").click(function() {
    alert("The button was clicked.");
  });
});

But it didn't alerted anything. Is there any way to pass the value from the button to the input value?

Share Improve this question edited Jan 4, 2016 at 13:19 Rajesh 25k5 gold badges50 silver badges83 bronze badges asked Jan 4, 2016 at 12:47 Konstantinos NatsiosKonstantinos Natsios 2,9249 gold badges41 silver badges77 bronze badges 2
  • 1 $("#btn").click(function(){ alert("The button was clicked."); }); this code should be put after $("#test1").append(appendeddatahtml); no need for document.ready – Just code Commented Jan 4, 2016 at 12:50
  • id's should be unique on a page – wirey00 Commented Jan 4, 2016 at 12:53
Add a ment  | 

5 Answers 5

Reset to default 6

Since you are adding it dynamically you need event delegation here. So just add click as below:

$(document).on('click',"#btn",function(){
    alert("The button was clicked.");
});

DEMO Here

It seems you have created the button dynamically via code during runtime and for such elements you need to use live or on events

http://api.jquery./on/

this one will help

example:

$(document).ready(function() {
    $(document).on('click', "#btn", function() {
        alert('here');
    });
});

working here just remove $(document).ready()

var test2 = 5 ;
appendeddatahtml = '<div class="test1"><button id="btn" type="button" value="'+test2+'">This is it!</button></div>';
 $("#test1").append(appendeddatahtml); 

  $("#btn").click(function(){
    alert("The button was clicked.");
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="latlon" id="latlon" style="display: none; " value="" />
<div id="test1"></div>

You have not created the element first.

You forgot $();

var test2 = 5 ;

 appendeddatahtml = $('<div class="test1"><button id="btn" type="button" value="'+test2+'">This is it!</button></div>');
   $("#test1").append(appendeddatahtml);

link to a working example of what you wanted is here

html code

<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="latlon" id="latlon" style="display: block; " value="" />
<div id="test1">
</div>

javascript code

$(document).ready(function() {
    var test2 = 5 ;
    var appendeddatahtml = '<div class="test1"><button id="btn" type="button" value="'+test2+'">This is it!</button></div>';
                        $("#test1").append(appendeddatahtml);
    $("#btn").click(function(){
        alert("The button was clicked. "+this.value);
        document.getElementById('latlon').value = this.value;
    });
});
发布评论

评论列表(0)

  1. 暂无评论