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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript function causes fields to disappear 2

Status
Not open for further replies.

alexisb

Programmer
Apr 5, 2001
100
US
Hi. I have a basic HTML page with many text boxes and drop-downs set up in a table (no form tags). The last drop-down has two choices. Based on that choice, the user is redirected to the next page. I created a Javascript function to handle the redirection and call it from the select tag, and it works fine. However, only that last drop-down appears now when I access the html page. All the previous text boxes and drop-downs do not appear.

I tested just adding the function, which I put between the <head>/</head> tags, and none of the fields in the form appear. Once I add the Javascript call to the select tags of the drop-down (see below) to redirect the user, that one drop-down appears but none of the other text boxes or drop-downs.

<select id="RentOrOwn" name="RentOrOwn" id="RentOrOwn" onChange="RedirectToNewPage()">
<option selected="selected" value="">--Select--</option>
<option value=" rent my property</option>
<option value=" own my property</option>
</select>

<Script language="javascript">
<!--
function RedirectToNewPage(){
//get the select box object
var objSel=document.getElementById("RentOrOwn");

//redirect the user
document.location.href=objSel.options(objSel.selectedIndex).value;
}
//-->
</Script>

Thanks for your help.
Alexis
 
I can't help feeling the problem is now with any of the code you've shown us. Do you have any other script on the page? Can you post a URL?

Whatever else you do, you should remove the duplicate ID from the select element.

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks for your response, Dan. The dup ID was just a typo. I've tested this so many different ways. I guess in the last iteration I pasted back the entire line instead of just the Javascript line. I removed it and the problem is still there. I really can't post the url, unfortunately. There are no other scripts on the page. It's really a basic HTML page with just text fields and drop-downs.

I figured the problem has to do with the Javascript function because the fields disappear as soon as I add the function.

Here's how it is before I add the function:
<html>
<head>
<title>Home Insurance Quote</title>
</head>
<body>

Here's how it is after. As soon as I paste this code and refresh, all the fields are gone. I even tried holding down the ctrl key to refresh JS.

<html>
<head>
<title>Home Insurance Quote</title>
<Script language="javascript">
<!--
function RedirectToNewPage(){
//get the select box object
var objSel=document.getElementById("RentOrOwn");

//redirect the user
document.location.href=objSel.options(objSel.selectedIndex).value;
}
//-->
</Script>
</head>
<body>

I am not a great JS or HTML codes. I know enough to get by.

Could this be because there is no form set up on this page? I didn't create the page but was asked the redirect logic piece. I did try setting up a form and it still didn't work.
Thanks,
Alexis
 
first of all, you'll need to change this:

Code:
document.location.href=objSel.options(objSel.selectedIndex).value;

to this:

Code:
document.location.href=objSel.options[objSel.selectedIndex].value;



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Thanks, cLFlaVA. I made the change but it's still not working.
 
I don't think so. I can see the next pages and they are correct, based on the drop-down choice. I've done more testing and, instead of doing the JS function, I built the logic into the onchange, as follows:

<select id="RentOrOwn" name="RentOrOwn" onchange="document.location=this.value">
<option selected="selected" value="">--Select--</option>
<option value=" rent my property</option>
<option value=" own my property</option>
</select>

I'm starting to think something's wrong with using JS on this server. I am going to move the code to my website and test, and see if it works. I'll post back after my test.
Thanks again.
Alexis
 
I tested from my website and the problem happens there also. Maybe it has to do with my computer. Could it be that my firewall is blocking JS scripts? I've worked on other projects with JS and I don't remember having a problem before but, then again, I did install Norton Internet Security 2006 within the last few weeks.
Thanks,
Alexis
 
1) js operates on your client computer, not the server.
2) js problems usually do not cause html to disappear.

give us the link to your site where it's not working.



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Oh this is a corker - I've seen one of these before, and it made my day when I found it ;-)

You are not closing your HTML comments correctly.

This:

Code:
<!--CONTACT INFO--!>

should be:

Code:
<!--CONTACT INFO-->


i.e. no exclamation mark at the end.

Fix that in all your comments and you'll be set.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks to both of you (with stars). I could have looked at this a million more times and never realized that. I did not do the HTML part. I just handled changing the last drop-down to redirect. I do my comments the way you said but I didn't notice that the guy who did the code didn't comment it correctly. I will tell him.

Thanks again. You have stopped my headache. I tried this so many different ways before I even posted my question.

Alexis
 
it doesn't matter, nobody ever bothers to read through that silly, pointless validator anyway.

Amen [angel] to that shizzle
headbang.gif


-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top