Alex van Rijs
Alex van Rijs

Reputation: 813

javascript function does not work

I am currently in the middle of the development of a website. If a user presses a button an javascript function needs to be called. I simplified this function to:

<html>
<head>
<script type="text/javascript">
function newProd(number,customer,software,hardware,name)
{
       //I simplified this function
       alert('number is: '+number+'customer is: '+customer+' software is: '+software+' hardware is: '+hardware+' name is: '+name);
}
</script>
</head>

<body>
     <input type="text" name="textfieldCustomer"><br>
     <input type="text" name="textfieldSoftware"><br>
     <input type="text" name="textfieldHardware"><br>
     <input type="text" name="textfieldDescription"><br>
     <input type="button" name="button" value="go to function" onClick="newProd('a number',textfieldCustomer.value,textfieldSoftware.value,textfieldHardware.value,textfieldDescription.value)">
</body>

when the user presses the button in Internet explorer, the function works perfectly! Unfortunately the function does not work in Chrome or Safari.

Does anyone have any idea what is going wrong?

Upvotes: 0

Views: 338

Answers (2)

Reporter
Reporter

Reputation: 3948

In my eyes there are two big mistakes in your code. First, the access to inputs fields is wrong. It needs a connection to an instance variable, like 'document'. The second one, the form tag is missing. If you wrap the input fields into a form tag you can access the values as Esailija has posted.

Upvotes: 0

Esailija
Esailija

Reputation: 140210

The form fields are not supposed to be defined as global variables. Maybe in IE they are but that's not a behavior you can depend on. Try this:

onClick="newProd('a number',
    this.form.textfieldCustomer.value,
    this.form.textfieldSoftware.value,
    this.form.textfieldHardware.value,
    this.form.textfieldDescription.value)">

Oh, and add a form to wrap the inputs of course.

Demo: http://jsfiddle.net/kdUMc/

Upvotes: 3

Related Questions