Reply
Function issue
Old 03-15-2008, 10:12 AM Function issue
KkillgasmM's Avatar
Ultra Talker

Posts: 361
Name: El Phantasmo
Location: England, north west
Ive recently bought a book to help me to learn Javascript. Theres a couple of issues Im having though, mainly when creating functions.

Here is a little practise script I made just to check the value of some text fields:


Code:
function validate (field)
    {
        value = field.value;
        window.alert (field + "'s value is <b>" + value + "</b>");
    }
and here is the HTML to call the script:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript" src="validate.js"></script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <p>
    <label>Field 1
    <input name="field1" type="text" id="field1" value="sdfsdfsd" size="20" maxlength="20" />
    </label>
  </p>
  <p>
    <label>Field 2
    <input name="field2" type="text" id="field2" value="23432424" size="20" maxlength="20" />
    </label>
  </p>
  <p>
    <input name="testField1" type="submit" id="testField1" value="Test Field 1" onclick="validate('field1');" />
    <input name="testField2" type="submit" id="testField2" value="Test Field 2" onclick="validate('field2');" />
  </p>
</form>
</body>
</html>
When I click any of the buttons, Im presented with a message similar to the following:
Code:
Field 1's value is undefined
If I specify in the script which text field I want to be checked, then it works. So I think the problem is in this bit of the script:

Code:
value = field.value;
But I cant see how I could fix it? I wanted to be able to pass arguments to the function to make it more flexible.
__________________
New Portfolio
KkillgasmM is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 03-15-2008, 12:18 PM Re: Function issue
logic ali's Avatar
Skilled Talker

Posts: 85
Quote:
Originally Posted by KkillgasmM View Post
Code:
onclick="validate('field1');"
If you put the name in quotes, you're passing a literal string not a field name. Alternatively you could pass
Code:
this.form['field1']
__________________
556E677261746566756C204261737461726473
logic ali is offline
Reply With Quote
View Public Profile
 
Old 03-15-2008, 12:28 PM Re: Function issue
KkillgasmM's Avatar
Ultra Talker

Posts: 361
Name: El Phantasmo
Location: England, north west
Ahhh I get ya! Thanks
__________________
New Portfolio
KkillgasmM is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Function issue
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.11529 seconds with 12 queries