Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Javascript and HTML function issue

<script stylesheet="text/javascript">
var statename;
function confirmReview(formObj){
 function myState( val ){
 statename = val;
 alert( "This is " + statename );
 }
 postcode = document.getElementById("postcode");
 //Folowing code
 if( statename== "Selangor" ){
 if( postcode < 10000 || postcode > 40000 )
 alert( "Invalid postcode for " + statename );
 }
}
</script>
<body>
<form name = reviewform onsubmit="return confirmReview(reviewform)" method=POST>
 <select id='state' onchange="myState(this.value);">
 <option>Specify state or territory</option>
 <option value="Selangor">Selangor</option>
 <option value="Terengganu">Terengganu</option>
 </select>
</form>
</body>

my question is how to store the value and save it to the statename as i fail to run in my HTML javascript code. can anyone teach?

i cannot print out my statename example if i want to make some comparison for my postcode when i load the script it run to the funciton . my HTML will just clear all the data like refresh the page without showing anything

after i edited the things senior suggested . it's still same. it cannot go to the alert that validation code with the statename

Answer*

Draft saved
Draft discarded
Cancel
6
  • @alex23 oops, sorry, forgot about that xD. Thanks for editing :) Commented May 12, 2013 at 8:40
  • 1
    @alex23 — If there are any such browsers, they are no longer in use. Browsers have defaulted to assuming JS forever and HTML 5 makes the attribute explicitly optional. Commented May 12, 2013 at 9:13
  • i cannot print out my statename example if i want to make some comparison for my postcode example Commented May 12, 2013 at 9:27
  • 1
    @Chirs, put the validation code (the code with the alert) in the myState function as well. Commented May 12, 2013 at 9:38
  • 1
    @Chirs, now the alert part is within function confirmReview(formObj) (good, I guess), but function myState is also defined within function confirmReview(formObj), hence will no longer be found by onchange="myState(this.value);" Also note that confirmReview should return true or false. See Show me a Simple javascript onSubmit handler. PLEASE NOTE: by changing your question, you've also made imulsion's answer outdated. That's not nice. Note we're not a forum; please read About and FAQ. Commented May 12, 2013 at 11:05

default

AltStyle によって変換されたページ (->オリジナル) /