1

I hava just a simple issue .

I wanna assign a value to a text box , but not as a string , as Java script function .

I will give you simple example :

     <input type="text" value="exam">//  here i assign the value as a string .





    <input type="text" value=functionx() >  // functionx is a java script function that        
          will assign a value to the text box.

     <script > function functionx(){return 'exam';}</script>

now after this simple example , my question is : how can i assign a value to the text box using java script function ?

0

1 Answer 1

5

You need to assign it in the script, you cannot call a function in the value attribute of the html.

Try this:

var text = document.getElementsByTagName('input')[0]; // would be better if you have a ID
text.value = functionx();

Demo here

My suggestion is to give a id to the input (like <input id="input_id" type="text" value="exam">) and then use getElementByID instead in my example.

In this case it could look like this demo.

Sign up to request clarification or add additional context in comments.

1 Comment

that is perfect ; yes , dont worry a bout ID , in my code of course there is ID , but this is example ,you know. this is right answer , thanx.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.