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

In my project i have 10 products for every product i have button called info, if i click on a info butt

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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744627425a4584663.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信