Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why doesn't my result show up ? 2

Status
Not open for further replies.

pmonett

Programmer
Sep 5, 2002
2,630
FR
Sorry to bother you with another beginner issue here, but I fail to see why this code doesn't work.
What should happen is that once the bet is validated in the function betcheck, the value should be shown in the div for control.
It doesn't. Why ?

Code:
<html>
<head>
	<script language="javascript">
		bet=0;
		purse=20;
		
		function betcheck(){
			var bForm;
			var result=false;
			var numericExpression = /^[0-9]+$/;
			
			bForm = document.userspace;
			if(bForm.betamount.value.length!=0){
				if(bForm.betamount.value.match(numericExpression)){
					if(bForm.betamount.value<=purse){
						bet=bForm.betamount.value;
						document.getElementById('checkbet').innerHTML=bet;
						result=true;
					}
				}
			}
			
			return(result);
		}
		function play(){
			document.getElementById('winresult').innerHTML='';
			if(betcheck){
				document.getElementById('winresult').innerHTML='go';
			}else{
				document.getElementById('betamount').value="";
				document.getElementById('winresult').innerHTML='no go';
			}
		}
	</script>
</head>
<body>
	
	<div id="play">
		<form id="userspace">
		Que voulez-vous parier ? <input type="text" id="betamount" name="betamount"/>
		<input type="button" onclick="play()" value="Play" />
		</form>
	</div>
	<div id="winresult">
	
	</div>
	<div id="checkbet">
	
	</div>
</body>
</html>

What I want to do, of course, is transfer the user input to an internal variable for the rest of the code. I fail to see what I'm doing wrong here.
Could anybody point me in the right direction ?

Pascal.

I've got nothing to hide, and I'd very much like to keep that away from prying eyes.
 
Got it - syntax error in form declaration.

This works :
Code:
<html>
<head>
	<script language="javascript">
		bet=0;
		purse=20;
		
		
		function go_play(){
			document.getElementById('checkbet').innerHTML=bet;
		}
		function betcheck(){
			var bForm;
			var bElement;
			var result=false;
			var numericExpression = /^[0-9]+$/;
			
			bForm = document.forms["userspace"];
			bElement = bForm.elements["betamount"];
			if(bElement.value.length>0){
				if(bElement.value.match(numericExpression)){
					if(bElement.value<=purse){
						bet=bElement.value;
						result=true;
					}
				}
			}
			return(result);
		}
		function play(){
			document.getElementById('winresult').innerHTML='';
			var cbet=betcheck();
			if(cbet==true){
				document.getElementById('winresult').innerHTML="go";
				setTimeout("go_play()",400);
			}else{
				document.getElementById('betamount').value="";
				document.getElementById('winresult').innerHTML="no go";
			}
		}
	</script>
</head>
<body>
	
	<div id="play">
		<form id="userspace">
		Que voulez-vous parier ? <input type="text" id="betamount" name="betamount"/>
		<input type="button" onclick="play()" value="Play" />
		</form>
	</div>
	<div id="winresult">
	
	</div>
	<div id="checkbet">
	
	</div>
</body>
</html>

So never mind :)

I've got nothing to hide, and I'd very much like to keep that away from prying eyes.
 
That will still only work under Internet Explorer though, because your form does not have a name, IE automatically uses the ID as a name if no name is present, other browsers require specifically a name so you can address them as document.formname or document.forms['formname'].





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Hi

I was close to write a similar reply, but I made a test first.
Phil said:
other browsers require specifically a name so you can address them as document.formname or document.forms['formname'].
Sadly Gecko, Presto, WebKit and KHTML also include the nameless forms in the [tt]forms[/tt] property and let the forms be picked by [tt]id[/tt]. So [tt]document[teal].[/teal]forms[teal][[/teal][green]"userspace"[/green][teal]][/teal][/tt] seems to work everywhere. [mad] ( At least with the current DOCTYPE. )

Feherke.
 
You are right, must be something implemented in newer versions of the bowser though not sure at which version this would have started, because for a long time I've tested this and it would not pick up with out a name in anything but IE.


However this brings to light a new problem which is when you have something like the folowing:

Code:
<form name="notmyform" id="myform">
	Form with ID
	</form>
	
	
	<form name="myform">
	Form with name
	</form>

There's going to be an issue accessing the second form with the name of "myform" because the first form with the ID "myform" takes precedence in the DOM. so document.forms.myform and document.forms['myform'] point to the first one always.

You are left with accessing the form through the numbered index such as document.forms[1].

In any case, and for future reference, disregard my last post as it is incorrect now.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Interesting comments. I will remember to always assign a name AND an Id.
Never too early to start best practices.

Thank you for your contributions !

Pascal.

I've got nothing to hide, and I'd very much like to keep that away from prying eyes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top