1 Replies - 430 Views - Last Post: 22 February 2012 - 09:48 AM

#1 yessssir12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-October 10

Javascript Submit, Focus, Field

Posted 22 February 2012 - 08:59 AM

<html> 
<head> 
</head> 
<body> 
<p>Exercise 5-3</p>
<script type = "text/javascript">

function sub() 
{	sumit = true
	

        var fir = document.getElementById("first")
	var fir1 = Number(fir); 
	alert(fir.value)
	if (fir.isNaN)
	{
		alert("bad");
		sumit = false;
	}
        var sec = document.getElementById("second")
	var sec1 = Number(sec);
	if (sec.isNaN)
	{
		alert("bad");
		sumit = false;
	} 
        var thir =  document.getElementById("third") 
	var thir1 = Number(thir);
	if (thir.isNaN)
	{
		alert("bad");
		sumit = false;
	}
        var x = document.getElementById("x") 
        var y = document.getElementById("y") 
        var z = document.getElementById("z")
	
	return sumit;

}

</script>

<form name = "f1" onsubmit="return sub()"action = "http://earl.rodd.us/dispform.php">
<p>

        <table border> 
        <tr><td>Price</td><td>quantity</td></tr> 
        <tr><td>4.56</td><td><input type = "text" name = "first" size = "10" value = 0></td></tr> 
        <tr><td>8.98</td><td><input type = "text" name = "second" size = "10" value = 0></td></tr> 
        <tr><td>14.16</td><td><input type = "text" name = "third" size = "10" value = 0></td></tr> 
        </table>

        <input type = "reset" id = "reset" /> 
        <input type = "submit" id = "sumit" /> 
        <input type = "hidden" name = "x" value = "4.56"> 
        <input type = "hidden" name = "y" value = "8.98"> 
        <input type = "hidden" name = "z" value = "14.16">

</form> 
</body>



This is what I have so far. It's supposed to display an alert if there is not a numeric value submitted into the form fields after the submit button is pushed. I cannot figure out how to put the first quantity field into focus. If there is an error, the focus needs to be in the field where the error occurred. I've tried looking through my web programming book and online googling but no luck.

Is This A Good Question/Topic? 0
  • +

Replies To: Javascript Submit, Focus, Field

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Javascript Submit, Focus, Field

Posted 22 February 2012 - 09:48 AM

To set the focus on a specific element, you just have to use the focus() function.
var field = document.getElementById("some_input_field");
field.focus();



A couple of comments on your code:

  • In order to get the value of a <input> field in Javascript, you would read the value property of the element.

  • Take a closer look at how the isNaN() function works. You code isn't using that concept correctly.

  • Your page is missing a Doctype. Those are not optional for HTML pages; there should be a Doctype at the very beginning of all HTML pages.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1