Member Avatar for HTMLperson5

I would like to write a page which accepts Value of textbox / input-field using javascript.
As you get values from this:

<script type="text/javascript">
var cheese=1
document.write(cheese)
</script>

Maybe the value cheese could be taken from text which the user has to enter....how?

Mmm... Cheese!

How about var myText = document.getElementById("myTextArea").value

You seem to be mixing up two different types of input: a textbox and a textarea box. Which one do you want to use?

You could do it both ways. The textbox allows for input of one variable at a time. "g0dzuki99" has suggested one way.

A textarea box could do it too. But it would be a little more complicated. If more than one entry was in the box, the default string would have to be broken up into its individual entries.

Member Avatar for HTMLperson5

Which one do you want to use?

Well, <textarea> is what i was hoping to use, anything which accepts text input from the person who is viewing the page.

Either one of those options could be put in a form.

If a single string only is going to be input, a text box would be the simplest approach.

"g0dzuki99" has suggested one way.

Myself, I usually put everything in a form and name all the variables.
For example, in the HTML form I might put something like the following:

Type of food you like to eat?: <input type = "text" name = "fav_food">

Then in the Javascript routine, which accepts the form (which I have named "dataForm"), assign the input string to the variable, say str1

function getString(dataForm){
. . .

var str1 = dataForm.fav_food.value;

. . .

} // End of function getString

Option 2: A textarea box

In the HTML form put something like the following:

<textarea name="inputData" rows="10" cols="75"></textarea>

Then in the Javascript routine, which accepts the form (which I have named "dataForm"), assign the input string to the variable, say textareaString:

function getString(dataForm){
 . . .

 var dataFormElements = dataForm.elements; // Reference to the form elements array.
 var textareaIndex = 0;  // The form array index for the textarea element--MIGHT NOT BE 0
 var textareaString = "";
  . . .
 textareaString = textareaString + dataFormElements[textareaIndex].value;

 . . .
 } // End of function getString 

Note that ANYTHING might be in a textarea box: text, numbers, multiple strings, etc. However, it gets passed to the function as one big long string.
If you only have a single entry in this field, you don't have to do anything else.
However, if you have to do any data cleanup, or break up multiple strings into their invidual entries, it gets more difficult.

Option 3: Enter input via a prompt:

For example,

function getString()
     {
     var str1 = prompt("What is your favorite food?", "");

  . . .

 } // End of function getString

yeah i agree with "DavidB" that what i usually used to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.