1
<!DOCTYPE html>
<html>
<head>
<title>Database test</title>
<link rel="stylesheet" type="text/css" href="test.css">
<script>
function validateForm(n)
{
var x=document.forms.SignUp.n.value;

alert("*"+x+"*");

}
</script>
</head>
<body>
<h1>Just a Database test</h1>
<form name="SignUp" action="http://127.0.0.1/cgi-bin/connectivity.cgi" onsubmit="return validateForm();" method="post">
Ename :<input type="text" name="ename" onchange="validateForm('ename');"><br><p id="error_1"></p>
<input type="submit" value="Send">

</form>
</body>
</html>

I am trying to pass the user input value by onchange="validateForm('ename') but it does not seem to work i dont get anything.

instead if i change function to this then it works but again i need to pass it as varaible from onchange="validateForm('ename').

function validateForm(n)
{

alert("*"+n+"*");

}
2
  • What? Both those functions are the same! Commented Sep 16, 2013 at 20:21
  • The solution is really simple, use an ID on your input or pass this, and not document.forms.formName.elementName Commented Sep 16, 2013 at 20:23

2 Answers 2

3

You should pass the element itself, which is this inside an inline handler:

validateForm(this);

function validateForm(elem)
    alert(elem.value);
}
Sign up to request clarification or add additional context in comments.

Comments

1

One way to make it work, without changing your function call:

function validateForm(n) {
    var x = document.forms.SignUp[n].value;
    alert("*"+x+"*");
}

Comments

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.