munish
munish

Reputation: 4634

passing variable to javascript function and using it in document.form?

<!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+"*");

}

Upvotes: 1

Views: 3422

Answers (2)

bfavaretto
bfavaretto

Reputation: 71908

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

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

Upvotes: 1

SLaks
SLaks

Reputation: 887275

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

validateForm(this);

function validateForm(elem)
    alert(elem.value);
}

Upvotes: 3

Related Questions