Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-09-2012, 07:20 AM   PM User | #1
buckminster83
New to the CF scene

 
Join Date: Dec 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
buckminster83 is an unknown quantity at this point
Submit to make active variable

Hi! New here, but was hoping to get this figured out in the next day or two.

I'm working on a project where the User enters a Zip code and on Submit, the Zip Code is available under the variable "zip".

I've been trying a lot of things, so either this coding is close or may not make sense at all. I'm using it to get the already stored "zip" from Yahoo's simpleWeather. At the bottom of this coding, the last line reads that I'm assigning "zip" the function "zipfx" in hope that "zip" will run the function which grabs the zip code entered from the form. I appreciate any help ASAP!

My current HTML:
<form name="zipSearch" action="" method="post">
<fieldset>
<input type="text" name="zipEntry" placeholder="Enter Zip Code" value=""/>
<input type="submit" name="zipSubmit" value="Search" onClick="zipfx()"/>
</fieldset>
</form>

My current JS:
function zipfx(){

var zipd= document.forms["zipSearch"]["zipEntry"].value;
return zipd;
}

var zip = zipfx();




---
buckminster83 is offline   Reply With Quote
Old 12-09-2012, 12:59 PM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Code:
var zip = zipfx();
This executes the function zipfx and assigns the return result to the variable zip. But it runs straight-away, before the form exists, so it returns an error as it cannot refer to forms['zipSearch'].

Code:
var zip = zipfx;
Assigns the function to the variable, so that it could be executed later by calling

Code:
zip();
FYI It is preferable and more modern to use IDs rather than names. In particular, form-names are deprecated/discouraged.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 12-09-2012, 01:49 PM   PM User | #3
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Maybe like this ...

Code:
 
<!doctype html>
<html>
<head>
<style>  
</style>
<script>
function zipfx(frm){  
 var zipid = frm.zipEntry.value;
 alert(zipid);
 return false;// prevents submit, return true to allow submit
}
</script>
</head>
<body>
 <form action="" method="post" onsubmit="return zipfx(this)">
  <fieldset>
   <input type="text" name="zipEntry" placeholder="Enter Zip Code" value=""/>
   <input type="submit" value="Search" />
  </fieldset>
 </form>
</body>
</html>
DaveyErwin is offline   Reply With Quote
Reply

Bookmarks

Tags
form, function, submit, variable, zip code

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 02:09 AM.


Advertisement
Log in to turn off these ads.