Programmer's Corner Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Programmer's Corner - Forums


Problem creating an expression with variables

 
Post new topic   Reply to topic    Programmer's Corner Forum Index -> HTML, CSS, VBScript, JavaScript
Author Message
blcArmadillo
Samurai++


Joined: 04 Mar 2005
Posts: 85
Location: Michigan

PostPosted: Wed Jul 25, 2007 6:24 pm    Post subject: Problem creating an expression with variables Reply with quote

I'm trying to make a simple function right now. I pass the form name and name of the input field I am targeting with the code:
Code:
onClick="clearfield('addboat', 'byear');"


This information passed to the function would than be used similar to the code listed below:
Code:
function clearfield(frm,fld) {
   var ele = document.frm.fld;
   alert(ele.value);
}


But, it appears as though document.frm.fld looks to a form called frm and an input field named fld rather than replacing the frm and fld with the information passed to the function. Can anyone tell me what I'm doing wrong? I can't seem to find anything online that shows me the correct syntax. Thanks.
Back to top
WannaBe
Wiggles


Joined: 22 Oct 2004
Posts: 714
Location: CA

PostPosted: Thu Jul 26, 2007 2:41 pm    Post subject: Reply with quote

something like this could work:

Code:
onclick="clearfield(document.addboat.byear);"


Code:

function clearfield(fieldItem)
{
   var itemValue = fieldItem.value;
   alert(itemValue)
}


Complete code (I used to test):
Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <title>Alert IT</title>
      <script>
function clearfield(fieldItem)
{
   var itemValue = fieldItem.value;
   alert(itemValue)
}
      </script>
   </head>
   <body>
   <form name="addboat">
   <input name="byear" type="text" value="" />
   <input type="button" value="Alert Value" onclick="clearfield(document.addboat.byear);" />
   </form>
   </body>
</html>



but if you do want to pass the names as strings, you could do a search in the document.forms array for the form named that then search in the found form for the field name.
Back to top
Ankou
Spam Mod


Joined: 22 Oct 2004
Posts: 1201
Location: Wisconsin

PostPosted: Sun Jul 29, 2007 5:14 pm    Post subject: Re: Problem creating an expression with variables Reply with quote

blcArmadillo wrote:
I can't seem to find anything online that shows me the correct syntax. Thanks.


I'll post this again (I left the link in the other post you had): http://www.onlinetools.org/articles/unobtrusivejavascript/chapter2.html

The link if to chapter 2 but read the whole thing. It does a great job of letting you know how to get what you're after. The best part is that it's pretty well written and once you understand it you'll be set for DOM playing of all sorts.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Programmer's Corner Forum Index -> HTML, CSS, VBScript, JavaScript All times are GMT - 5 Hours
Page 1 of 1

 


Powered by phpBB © 2001, 2002 phpBB Group