0

I know that you cannot really have variables in html, but I'm wondering if this is possible. I have searched around and cannot find anything that clearly answers my question. Hopefully someone hear can point me in the right direction. Here's what I have in mind:

<!DOCTYPE html>
<html>
<body>

<input type="text" name="test">
<input type="submit" onclick="myFunction(test)">
<script type="text/javascript">
function myFunction(test)
{
alert("Welcome " + test);
}
</script>

</body>
</html>

Would this work or something like it? Thanks, Sam

4
  • What precisely are you trying to accomplish? Commented Aug 16, 2012 at 15:22
  • To answer your question, no, that would not work, as test doesn't have any value. Commented Aug 16, 2012 at 15:22
  • to alert the value of the text input, sorry for being unclear Commented Aug 16, 2012 at 15:23
  • 2
    javascript can grab the values of inputs from the document. Read up on how to do this. Commented Aug 16, 2012 at 15:23

2 Answers 2

3

If you mean using the contents of an input as a variable:

<!DOCTYPE html>
<html>
<body>

<input type="text" id="some_id" name="test">
<input type="submit" onclick="myFunction(document.getElementById('some_id').value)">
<script type="text/javascript">
function myFunction(test)
{
alert("Welcome " + test);
}
</script>

</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

What I understood from your question is you want to access <input> and send it to function(test) try this:

<input type="text" name="test" id="test"> <-- Give ID here
<input type="submit" onclick="myFunction(test)"> <--Send same ID here
<script type="text/javascript">
function myFunction(test)
{
alert("Welcome " + test.value);
}
</script>

</body>

2 Comments

don't you want to pass document.getElementById('test') to myFunction()?
@Matt the question was unclear, as I have said I have answered what I understood from question. I thought the guy wanted to send test as parameter to his method to get it's value.

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.