Form submit to javascript passes integer

Drake2007

Expert Member
Joined
Oct 23, 2008
Messages
4,413
Ok I'm missing something here.

Code:
<form name="add_point">
<input type="text" value="" name="postcode" size="8" />
<input type="submit" value="Place Marker" onclick="javascript:
    initialize(add_point.postcode.value)" />

Code:
function initialize(x){
    console.log(x);

Now when I submit some text I get an integer in the console.
Is there some trick to return the string instead?
I'm open to suggestions if there's a better way to do this but it has to be in javascript.
 

graviti

Senior Member
Joined
May 8, 2006
Messages
665
Change the console.log(x); to console.log(""+x); ???

Not really sure, not much of a javascripter, but it is possible that it's outputting the memory reference for x, and not it's value. The "" + should enforce the printing of the value. Just an idea, especially since I don't do javascript
 

Drake2007

Expert Member
Joined
Oct 23, 2008
Messages
4,413
Yeah, looks like it is memory ref.

The page is reloading when the submit is pressed which might explain why I'm losing the string.

Thanks.
/me goes to pull hair out some more :p
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
Change the type="submit" to type="button" and the form won't submit upon the click event... You can manually submit the form afterward in your function by calling form.submit().
 

Drake2007

Expert Member
Joined
Oct 23, 2008
Messages
4,413
Many ways to skin the cat with subtle differences,

onsubmit="return false" seems the way to go, especially if the user pressed enter instead of clicking the button.

Thanks Acid
 

Drake2007

Expert Member
Joined
Oct 23, 2008
Messages
4,413
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

she no like.

Code:
<body onsubmit="return false;">

Attribute "onsubmit" exists, but can not be used for this element.

so the jury is still out.
 
Top