-3

I need to put a button called add. After the user clicks, it should change to added.

<html>
<body>
     <script>
     function one()
     {
         document.write("Added");
     }    
     </script>                   
     <input type="button" value="Add" onclick="one()">
</body>
</html>

but this code replaces all the content in my page and says "added" while I need to change only the button text, without any changes in the background.

RahulD
  • 699
  • 2
  • 16
  • 38

2 Answers2

4

Do this:

<html>
<body>                   
<input type="button" value="Add" onclick="this.value='Added'">
</body>
</html>

JSfiddle: http://jsfiddle.net/7w5AQ/

pattyd
  • 5,659
  • 10
  • 37
  • 57
1

Your document.write is doing much more than you want it to.

You can do just this:

<html>
    <head>
     <script>
         function one(element)
         {
             //Your other javascript that you want to run
             // You have 'element' that represents the button that originated the click event and you can just do the next line to change the text
             element.value = "Added";
         }    
         </script>             
    </head>
    <body>

         <input type="button" value="Add" onclick="one(this);">
    </body>
</html>
Filipe Silva
  • 20,688
  • 4
  • 50
  • 67