Hi I have started this code but i cant get it to work. new to javascript, but not sure how it works. im trying to make an alert for a text box "rrp" that alerts when it is less than the list price, therefore stopping an incorrect price being lodged.

here is what i have...

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<!-- Start of FORM -->
<form method="post" action="">
<script type="text/javascript">
function margin()
{
var LIST = document.getElementById("list").value
var RRP = document.getElementById("rrp").value
if(LIST>RRP)
{
alert("The list price is greater than the rrp")
}
</script>
<!--this list price is echo'd from database -->
<input type="text" name="list" value="10" id="list">

<!--this list price is echo'd from database -->
<input type="text" name="rrp" value="" id="rrp" onupdate="margin()">
<input type="submit" name="submit" value="submit">
</form>
<!-- End of FORM -->
</body>
</html>

remember im just a learner, just learnt a bit of php but would like to start writing in javascript

Recommended Answers

All 2 Replies

Gotboots,

Your code looks great, but there are quite a few errors...

  • Your function 'margin()' is missing a closing curly brace '}'.
  • 'onupdate' isn't an input element event, AFAIK. Try 'onchange' instead.
  • The input element values are of type string, so your conditional test will not work as expected. Try converting the strings to integer values with the parseInt() function, e.g. var LIST = parseInt(document.getElementById("list").value);
  • Note that javascript statements end with a semicolon. You're missing about three.

The doctype declaration in your HTML isn't quite right too. Which development environment are you using? Find one that supports syntax checking, HTML validation, and code completion. This'll help you to reduce errors and should make learning HTML or JavaScript a little easier.

i have done this with jquery because its a quick and easily approach and very much same to JS.hope you like this let me known

<html>

<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){
		
	$('#submit').click(function(){
	
	var listValue = $('#list').val();
	var rrpValue = $('#rrp').val();

	
	if(parseInt(listValue)>parseInt(rrpValue))
	{
		
		alert("List value greater than rrp");
		
	}
	
						   
});
	
	});
</script>
</head>
<body>
<!-- Start of FORM -->

<form  action="">

<!--this list price is echo'd from database -->
<input type="text"  id="list" value="10"/>

<!--this list price is echo'd from database -->
<input type="text"  id="rrp" />
<input type="submit"  id="submit" value="submit" />
</form>
<!-- End of FORM -->
</body>
</html
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.