It is possible to pass a value from javascript to html by writing it to a tag. However, if I would wish to pass a user defined value (etc, entered by the person viewing the webpage) to java script so I can do things with it, what would be the most easiest way possible?
At the moment, I have something like this:
<div class="entry foreground-color">
<form>
<input type="text" name="mands" size="60"/>
</form>
</div>
How can I make the value from the form be passed to my javascript?
Or, am I going in a totally wrong direction? If so, what would be the correct way to get user input, and pass it on to javascript?
EDIT: Apologies for my misuse of terminology. I am making a text based adventure game, and I want the user to be able to type in a response, press enter, and have the response be sent to javascript, so I can use javascript to evaluate the response (etc "go south", "go north"), and write back to the element with the new situation (etc "as you went south, you found a troll").
It is possible to pass a value from javascript to html by writing it to a tag. However, if I would wish to pass a user defined value (etc, entered by the person viewing the webpage) to java script so I can do things with it, what would be the most easiest way possible?
At the moment, I have something like this:
<div class="entry foreground-color">
<form>
<input type="text" name="mands" size="60"/>
</form>
</div>
How can I make the value from the form be passed to my javascript?
Or, am I going in a totally wrong direction? If so, what would be the correct way to get user input, and pass it on to javascript?
EDIT: Apologies for my misuse of terminology. I am making a text based adventure game, and I want the user to be able to type in a response, press enter, and have the response be sent to javascript, so I can use javascript to evaluate the response (etc "go south", "go north"), and write back to the element with the new situation (etc "as you went south, you found a troll").
Share Improve this question edited Mar 1, 2014 at 15:10 asked Mar 1, 2014 at 15:04 user3367792user3367792 2- There are a number of ways of doing it. Do you want to access the input when the form is submitted or at any time? – Mathias Commented Mar 1, 2014 at 15:07
- 1 by "passed" what do you mean ? Stored into a variable or executed like the eval function ? – Ifnot Commented Mar 1, 2014 at 15:07
7 Answers
Reset to default 4You can just stop the form from submitting and get the value:
HTML:
<div class="entry foreground-color">
<form onsubmit="return getValue('mands')">
<input type="text" name="mands" size="60" id="mands"/>
</form>
</div>
JavaScript:
function getValue (id) {
text = document.getElementById(id).value; //value of the text input
alert(text);
return false;
}
Fiddle: Fiddle
If you want to clear the box afterwards, use:
document.getElementById(id).value = '';
Like so:
function getValue (id) {
text = document.getElementById(id).value; //value of the text input
alert(text);
document.getElementById(id).value = '';
return false;
}
The input value is already available for Javascript via DOM API:
document.getElementsByName( "mands" )[0].value
You can get the value of your input control using:
document.getElementsByName("mands")[0].value;
Since getElementsByName()
method returns an Array
of elements with specified name, you will need to take the first element assuming that there is only one elements with name
attribute mands
.
Instead of that, for simplicity and uniqueness, i suggest you use the famous way to achieve that using id
attribute and getElementById()
method.
<input type="text" name="mands" size="60" id="mands"/>
var input = document.getElementById("mands").value;
or
document.getElementsByName( "mands" )[0].value
now do anything with this
To get the value of an HTML element, first, you need to get the desired element (you can use theses methods):
- getElementById
- getElementsByTagName
- getElementsByClassName
- querySelector (moderns browsers)
- querySelectorAll (moderns browsers)
Theses methods depending on document
(documentation).
So in your case you can try something like this:
var inputs = document.getElementsByTagName('input'),
mandValue = null;
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].name === "mands") {
mandValue = inputs[i].value; // get the element value
}
}
alert (mandValue); // show the value
But you need to set a "catcher" on the form default action. So:
<form>
Bee:
<form onsubmit="return getValue()">
And you set the javascript code above in the getValue function:
function getValue() {
var inputs = document.getElementsByTagName('input'),
mandValue = null;
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].name === "mands") {
mandValue = inputs[i].value; // get the element value
}
}
alert (mandValue); // show the value
return false; // prevent default form action
};
You are in the right path :)
For example you can do something like this (see http://jsfiddle/ahLch/):
HTML:
<h1 id="mandsExample"></h1>
<div class="entry foreground-color">
<form>
<input type="text" id="mands" size="60"/>
</form>
</div>
JavaScript:
var input = document.getElementById('mands');
var example = document.getElementById('mandsExample');
input.addEventListener('change', function () {
example.innerHTML= input.value;
});
A couple of things to note:
If you are new to JavaScript, and you wish to make your code cross browser (especially if you want to target old versions of IE), take a look to jQuery.
If you wish to learn how to use plain DOM APIs provided by the browser without the jQuery layer, take a look to: http://youmightnotneedjquery./ (it's very useful once that you learned jQuery basics).
There are a couple of ways to get the value:
Intercepting from submmit event: I don't remend it, you have to take care of avoiding the default submit behavior, and you have to create a form around your input field. That was necessary in old browsers, but today is not.
change event: it's fired when the input value has changed and the input looses the focus (is the usual event used for form validation).
keydown and keyup: they give you more control, by capturing each keystroke, but it's lower level than the change event. For a plete reference see: https://developer.mozilla/en-US/docs/Web/API/KeyboardEvent
Use "id" instead of "name". Name is only necessary when you want to submit the value, in new browsers you can leave just input tag without a form.
Getting the value of the input tag is quite easy with jQuery. (I take it you need this anyway to actually send the message via AJAX to a server..?)
$("#idofinput").val(); will copy the value, $("#idofinput").val(''); will empty it.
You probably want to do this without actually submitting a form. Recreating one in javascript isn't to hard. For the submit on enter you can use something like this:
function checkEnter(e)
{
var keynum;
if(window.event) // IE8 and earlier
{
keynum = e.keyCode;
}
else if(e.which) // IE9/Firefox/Chrome/Opera/Safari
{
keynum = e.which;
}
if(keynum==13)
{
sendMessage();
}
}