Solved

javascript not working

Posted on 2011-09-08
4
200 Views
Last Modified: 2012-05-12
I write the simple form display(hide and show) use in javascript..this form output not display in google chrome..IE and firefox working correctly,. but chrome not display the output..what is the mistake in my program..plz send me comments..here i paste in my html and js file..
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Dynamic Order Form</title>
<script language="JavaScript" type="text/javascript"
src="dform.js">
</script>
</head>
<body>
<h1>Order Form</h1>
<hr>
<form name="form1">
  <b>Bill to:</b><br>
  <b>Name:</b>
  <input type="text" name="customer" size="20" id="txt">
  <br>
  <b>Address 1:</b>
  <input type="text" name="addr1" size="20">
  <br>
  <b>Address 2:</b>
  <input type="text" name="addr2" size="20">
  <br>
  <b>City:</b>
  <input type="text" name="city" size="15">
  <b>State:</b>
  <input type="text" name="state" size="4">
  <b>Zip:</b>
  <input type="text" name="zip" size="9">
  <hr>
  <b>Ship to:</b><br>
  <input type="radio" name="shipopt" value="same" checked onClick="Show(0);">
  <b>Same Address</b>
  <input type="radio" name="shipopt" value="other" onClick="Show(1);">
  <b>Other Address</b>
  <div ID="shipto" style="display: none;"> <br>
    <b>Name:</b>
    <input type="text" name="shipname" size="20">
    <br>
    <b>Address 1:</b>
    <input type="text" name="shipaddr1" size="20">
    <br>
    <b>Address 2:</b>
    <input type="text" name="shipaddr2" size="20">
    <br>
    <b>City:</b>
    <input type="text" name="shipcity" size="15">
    <b>State:</b>
    <input type="text" name="shipstate" size="4">
    <b>Zip:</b>
    <input type="text" name="shipzip" size="9">
  </div>
  <hr>
  <a href="#" onClick="toggle_visibility('foo');">Click here to toggle visibility of element #foo</a>
  <div id="foo">This is foo</div>
  <div ID="items"> <b>Qty:</b>
    <input type="text" name="qty" size="3">
    <b>Item:</b>
    <input type="text" name="item" size="45">
    <br>
    <input type="button" value="Add an Item" onClick="AddItem();" ID="add">
  </div>
  <hr>
  <input type="submit" value="Continue..." onClick="display();">
</form>
</body>
</html>
__________________________________________________________
// global variable
var items=1;
function AddItem() {
if (!document.getElementById) return;
// Add an item to the form
div=document.getElementById("items");
button=document.getElementById("add");
items++;
newitem="<b>Qty: </b>";
newitem+="<input type=\"text\" name=\"qty" + items;
newitem+="\" size=\"3\"> ";
newitem+="<b>Item: </b>";
newitem+="<input type=\"text\" name=\"item" + items;
newitem+="\" size=\"45\"><br>";
newnode=document.createElement("span");
newnode.innerHTML=newitem;
div.insertBefore(newnode,button);
}
function display()
{
	var dis;
	dis=document.form1.customer.value+ "</br>";
	dis+=document.form1.addr1.value+ "</br>";
	dis+=document.form1.addr2.value+ "</br>";
	dis+=document.form1.city.value+ "</br>";
	dis+=document.form1.state.value+ "</br>";
	dis+=document.form1.zip.value+ "</br>";
	obj=document.getElementById("shipto");
	if(obj.style.display=="block") 
	{
	dis+=document.form1.shipname.value+ "</br>";
	dis+=document.form1.shipaddr1.value+ "</br>";
	dis+=document.form1.shipaddr2.value+ "</br>";
	dis+=document.form1.shipcity.value+ "</br>";
	dis+=document.form1.shipstate.value+ "</br>";
	dis+=document.form1.shipzip.value+ "</br>";
	document.write(dis);
	document.close();
	}
	else
	{
	document.write(dis);
	document.close();
	}	

}
function toggle_visibility(id) {
       var e = document.getElementById(id);
      if(e.style.display == 'block')
         e.style.display = 'none';
      else
       e.style.display = 'block';
}
function Show(a) {
//if (!document.getElementById) return;  //hide i
//Hide or show ship-to address
obj=document.getElementById("shipto");
if (a) obj.style.display="block";
else obj.style.display="none";
}

Open in new window

0
Comment
Question by:codeoxygen
  • 2
4 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 36508106
change:
<script language="JavaScript" type="text/javascript"
src="dform.js">
</script>

To:
<script type="text/javascript" src="dform.js"></script>

===========
change:
<a href="#" onClick="toggle_visibility('foo');">

to:
<a href="#" onclick="return toggle_visibility('foo');">

===========
function toggle_visibility(id) {...}

needs return false; at the end

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Dynamic Order Form</title>
<script type="text/javascript" >
// global variable
var items=1;
function AddItem() {
if (!document.getElementById) return;
// Add an item to the form
div=document.getElementById("items");
button=document.getElementById("add");
items++;
newitem="<b>Qty: </b>";
newitem+="<input type=\"text\" name=\"qty" + items;
newitem+="\" size=\"3\"> ";
newitem+="<b>Item: </b>";
newitem+="<input type=\"text\" name=\"item" + items;
newitem+="\" size=\"45\"><br>";
newnode=document.createElement("span");
newnode.innerHTML=newitem;
div.insertBefore(newnode,button);
}
function display()
{
	var dis;
	dis=document.form1.customer.value+ "</br>";
	dis+=document.form1.addr1.value+ "</br>";
	dis+=document.form1.addr2.value+ "</br>";
	dis+=document.form1.city.value+ "</br>";
	dis+=document.form1.state.value+ "</br>";
	dis+=document.form1.zip.value+ "</br>";
	obj=document.getElementById("shipto");
	if(obj.style.display=="block") 
	{
	dis+=document.form1.shipname.value+ "</br>";
	dis+=document.form1.shipaddr1.value+ "</br>";
	dis+=document.form1.shipaddr2.value+ "</br>";
	dis+=document.form1.shipcity.value+ "</br>";
	dis+=document.form1.shipstate.value+ "</br>";
	dis+=document.form1.shipzip.value+ "</br>";
	document.write(dis);
	document.close();
	}
	else
	{
	document.write(dis);
	document.close();
	}	

}
function toggle_visibility(id) {
       var e = document.getElementById(id);
      if(e.style.display == 'block')
         e.style.display = 'none';
      else
       e.style.display = 'block';
return false;
}
function Show(a) {
//if (!document.getElementById) return;  //hide i
//Hide or show ship-to address
obj=document.getElementById("shipto");
if (a) obj.style.display="block";
else obj.style.display="none";
}
</script>
</head>
<body>
<h1>Order Form</h1>
<hr>
<form name="form1">
  <b>Bill to:</b><br>
  <b>Name:</b>
  <input type="text" name="customer" size="20" id="txt">
  <br>
  <b>Address 1:</b>
  <input type="text" name="addr1" size="20">
  <br>
  <b>Address 2:</b>
  <input type="text" name="addr2" size="20">
  <br>
  <b>City:</b>
  <input type="text" name="city" size="15">
  <b>State:</b>
  <input type="text" name="state" size="4">
  <b>Zip:</b>
  <input type="text" name="zip" size="9">
  <hr>
  <b>Ship to:</b><br>
  <input type="radio" name="shipopt" value="same" checked onClick="Show(0);">
  <b>Same Address</b>
  <input type="radio" name="shipopt" value="other" onClick="Show(1);">
  <b>Other Address</b>
  <div ID="shipto" style="display: none;"> <br>
    <b>Name:</b>
    <input type="text" name="shipname" size="20">
    <br>
    <b>Address 1:</b>
    <input type="text" name="shipaddr1" size="20">
    <br>
    <b>Address 2:</b>
    <input type="text" name="shipaddr2" size="20">
    <br>
    <b>City:</b>
    <input type="text" name="shipcity" size="15">
    <b>State:</b>
    <input type="text" name="shipstate" size="4">
    <b>Zip:</b>
    <input type="text" name="shipzip" size="9">
  </div>
  <hr>
  <a href="#" onclick="return toggle_visibility('foo');">Click here to toggle visibility of element #foo</a>
  <div id="foo">This is foo</div>
  <div ID="items"> <b>Qty:</b>
    <input type="text" name="qty" size="3">
    <b>Item:</b>
    <input type="text" name="item" size="45">
    <br>
    <input type="button" value="Add an Item" onClick="AddItem();" ID="add">
  </div>
  <hr>
  <input type="submit" value="Continue..." onClick="display();">
</form>
</body>
</html>

Open in new window

0
 

Author Comment

by:codeoxygen
ID: 36508237

Now function toggle_visibility(id) {...} working good,. but display() function not working in google chrome and data not print in another page(document.write(dis)) in google chrome...
0
 
LVL 82

Accepted Solution

by:
hielo earned 250 total points
ID: 36511815
Try:
 <input type="button" value="Continue..." onClick="display();">
0
 
LVL 9

Expert Comment

by:user_n
ID: 36512583
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

CSS3 Custom checkboxes This article shows how to style the checkbox form element using only CSS. Works in: Chrome, FF, Safari, Opera, IE9+ Uses modernizr.js to check for :checked pseudo class, falling back to plain old checkboxes (IE8 and bel…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now