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

Can anyone tell me why this is not functioning?

Status
Not open for further replies.

dropd27

Technical User
Joined
Jan 25, 2006
Messages
2
Location
US
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<script language="JavaScript"><!--
function settestValue() {
var selectedItem = document.test.shift.selectedIndex;
var selectedItemValue = document.test.shift.options[selectedItem].value;
var selectedItemText = document.test.shift.options[selectedItem].text;
}
//--></script>
<form name="test">
<SELECT name="shift" onChange="settestValue()">
<OPTION VALUE="303000L">left drop universal</option>
<OPTION VALUE="303001R">right drop universal</option>
<OPTION VALUE="303002L">TJ automatic</option>
<OPTION VALUE="303002U">TJ universal</option>
<OPTION VALUE="303003L">TJ manual
<OPTION VALUE="303004L">Bronco</option>
<OPTION VALUE="303005L">ZJ</option>
<OPTION VALUE="303006L">Explorer</option>
<OPTION VALUE="303007L">XJ</option>
<OPTION VALUE="303009">universal cable (+$145)</option>

</SELECT>
</form>
<SCRIPT Language="JavaScript">
document.write(selectedItemValue)
</SCRIPT>


</BODY>
</HTML>
 
First, your variable selecteItemValue is local to the function settestValue, so your document.write statement will probably produce 'undefined'.

Second, you write the value to the page when it loads, but don't call the function until after something on the dropdown list is changed. That's like waiting until 9 a.m. in the morning to decide you wanted to get up at 8 instead, then setting your alarm for 8 a.m. the same morning. The time is past, the opportunity is gone.

That said, if you do a document.write in the function, you're going to blow the page away with the value it writes. Try putting an alert in the function like this:

alert(selectedItemValue);

right before the function closes.

Lee
 
1) The document.write is only going to be called on the initial page load
2) The document.write has absolutely no clue what value selectedItemValue has because it's a variable that is local only to the settestValue() function
3) The document.write has absolutely no clue what value selectedItemValue has (again) because when the document.write is called (on page load) the function has not even been executed (doesn't happen till the select box is changed) (I'm pointing this out just in case you try to give the selectedItemValue variable global scope to try and remedy that problem.)
4) if you put he document.write in your function so that it will recognize what value is inside selectedItemValue, it will overwrite all the rest of the HTML that is currently on the page when the function is called - so that's not gonna work either

conclusion:

What you're trying to do is not gonna work - period.

If you want same way to seewhat is in the selectedItemValue variable when it is defined, then use an alert:
Code:
function settestValue() {
    var selectedItem      = document.test.shift.selectedIndex;
    var selectedItemValue = document.test.shift.options[selectedItem].value;
    var selectedItemText  = document.test.shift.options[selectedItem].text;
    [!]alert(sectedItemValue);[/!]
}

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
I basically like to copy everything Lee says.

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
thanks everyone for the info, as you can obviously tell I'm new to this stuff.
 
Thanks for backing me up, kaht. :)# It's nice to get the support of someone on the expert list.

Lee
 
It was inadvertent really, you were quicker to the submit button. But it's amazing how great minds think alike [thumbsup2]

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top