Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Old 03-15-2008, 11:12 AM Function issue
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is online now
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
 
Register now for full access!
Old 03-15-2008, 01:18 PM Re: Function issue
logic ali's Avatar
Super Talker

Posts: 104
Trades: 0
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']
logic ali is offline
Reply With Quote
View Public Profile
 
Old 03-15-2008, 01:28 PM Re: Function issue
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Ahhh I get ya! Thanks
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is online now
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Reply     « Reply to Function issue
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB 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.11606 seconds with 12 queries