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

javascript - How to populate input text field data based on button pressed - Stack Overflow

programmeradmin9浏览0评论

In my project i have 10 products for every product i have button called info, if i click on a info button a form popups in that i wanted to fill the first field(product name) automatically ....lets say for product soap i have info button

                  `<a href="" id="1" class="btn">info</a>`

In the form

               `<form>
            <input type="text" id="product_name">
           <input type="text" id="number">
     </form>`

I want the field Product_name to be filled automatically based on button pressed so how to get this ..plz help i tried using <a href="" id="1" onClick="reply_click(this.id)">info</a> and my js

function  reply_click(clicked_id)
{
//alert(clicked_id);
 if(clicked_id == "1")
 {
  document.getElementById(product_name).value='Soap';   
 }
 else
 {
 alert("button not pressed");
 }

 }

i tried this logic for example, i am able to read button click but iam unable to write to form... some one please help me out Thank you in advance

In my project i have 10 products for every product i have button called info, if i click on a info button a form popups in that i wanted to fill the first field(product name) automatically ....lets say for product soap i have info button

                  `<a href="" id="1" class="btn">info</a>`

In the form

               `<form>
            <input type="text" id="product_name">
           <input type="text" id="number">
     </form>`

I want the field Product_name to be filled automatically based on button pressed so how to get this ..plz help i tried using <a href="" id="1" onClick="reply_click(this.id)">info</a> and my js

function  reply_click(clicked_id)
{
//alert(clicked_id);
 if(clicked_id == "1")
 {
  document.getElementById(product_name).value='Soap';   
 }
 else
 {
 alert("button not pressed");
 }

 }

i tried this logic for example, i am able to read button click but iam unable to write to form... some one please help me out Thank you in advance

Share Improve this question asked Feb 28, 2014 at 18:41 fabs11fabs11 171 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You are using id without quotes product_name but 'product_name'

should be

 document.getElementById('product_name').value='Soap';   

instead of

  document.getElementById(product_name).value='Soap';   

As stated by Suman, you need to provide 'product_name' as a string to getElementById(). Here is a working example of your approach: http://jsfiddle/CTnFt/

But since you will likely have many of these tags for many products, a simpler solution might be to place the value in a data- attribute of your tag and read it in your javascript function, instead of a larger, harder to maintain collection of if/elses. Here is a working example: http://jsfiddle/Rc9MG/

<a href="#" id="1" class="btn" onclick="reply_click(this)" data-product-name="Soap">info</a>

<form>

    <input type="text" id="product_name" />

</form>

<script type="text/javascript">

    function reply_click(element)
    {
        document.getElementById('product_name').value = element.getAttribute('data-product-name');
    }    

</script>
发布评论

评论列表(0)

  1. 暂无评论