HTML form + Javascript - how to do it?

phinix

Gawd
Joined
Dec 13, 2005
Messages
884
To explain it as simple as it is:
I have special characters buttons on website, lets say 20 different buttons with french alphabet special characters.
Now, I have input text field, where user tapes his answer to a question listed above. When he needs to use special character he clicks on the approprietsfsf button with that character, and it appears in text field after the string he taped.
problem I have is - how to do it for all button? I can direct the value of one button to that field by simply "MyElement.value = MyElement.value + button.value;"
But now how can I add it when user clicks different button? How to change that button.value to actually button id that he clicked?

OK.. here's what I've got so far:

list of buttons (just few here to show how they look)
Code:
 <input type='button' value="&aacute;" id="aacute" onclick="AddChar()"/> 
  <input type='button' value="&agrave;" id="agrave" onclick="AddChar()"/>  
  <input type='button' value="&acirc;" id="acirc" onclick="AddChar()"/> 
  <input type='button' value="&auml;" id="auml" onclick="AddChar()"/>

.. text field:
Code:
<input type="input" id="myfield"></input>

Now, the function:
Code:
function AddChar()
{
   var MyElement = document.getElementById("myfield");
   MyElement.value = MyElement.value + aacute.value;

   return true;
}


So question is - how to change that "aacute.value" to "clickedbutton".value?
it has to be easy, but I'm total noob in javascript, so please help me out here!:D
 
do this.
change you button onclick to onclick="AddChar(this)"

then, change AddChar to

function AddChar(obj)

then change the MyElement.value line to

MyElement.value = MyElement.value + obj.value

what this does is send the object that you clicked to the function so you can access properties of that object (IE the button that was clicked)
So no matter what button is clicked, the function will get the value.
 
Back
Top