-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
  • 709
  • 2
  • 16
  • 39
  • By 'change...font' do you mean you want to change the words on the button (from 'add' to 'added'), or you want to change the font-family (from Arial to Times New Roman (for example))? – David Thomas Jul 20 '13 at 16:15
  • He means text, not font I think – pattyd Jul 20 '13 at 16:54

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,927
  • 11
  • 38
  • 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
  • 21,189
  • 5
  • 53
  • 68