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

Check that an item i selected 2

Status
Not open for further replies.

vpekulas

Programmer
Jan 8, 2002
154
CA
Hello,


I have a select (<select></select>) element which is populate from the DB, each item has a numeric value.
When I click the preview button the ID gets passed to an iframe.

My question is: How can I check that an item was selected
before I send it to the iFrame ?

Code:
function PreviewItem(){   
    previewframe.window.location.replace("set_template_preview.asp?ID=" + frm.tid.options[frm.tid.selectedIndex].value);            
    return false 
}

<input style="width: 150px;" type="Button" value="Preview Template" onclick="return PreviewItem()" />


Thanks for your time.

&quot;Taxes are the fees we pay for civilized society&quot; G.W.
 
I suppose you could set the top option text to 'Please select' with a value of ''. Your function can then check if the select element value is '' or not. If not then an option has been selected.

ASCII silly question, get a silly ANSI
 
Yes, I can do that easily in ASP, but I need it in JS, so I don't get the JS error when the preview button is pressed.
I get error:

Object doesn't support this property or method
on line: previewframe.window.location.replace("set_ ...... etc.

Thanks again.

&quot;Taxes are the fees we pay for civilized society&quot; G.W.
 
This will check for the null value in JS. Hope I'm on the right track:

function PreviewItem(){
if(frm.tid.options[frm.tid.selectedIndex].value!=''){
previewframe.window.location.replace("set_template_preview.asp?ID=" + frm.tid.options[frm.tid.selectedIndex].value);
}
return false;
}

ASCII silly question, get a silly ANSI
 
Thanks for your answer ! Strangly it still didn't help :(
It now checks for value, but I still get the error

Object doesn't support this property or method
Line: if(frm.tid.options[frm.tid.selectedIndex].value!=''){

Any idea why ? Is there somthing like ON ERROR RESUME NEXT in JavaScript ?

&quot;Taxes are the fees we pay for civilized society&quot; G.W.
 
frm.tid looks suspect. Could you show the scripting that sets the object 'frm'?

ASCII silly question, get a silly ANSI
 
Here is the entire thing as used on the page:

Code:
<script language="JavaScript">
function PreviewItem(){ 
    if(frm.tid.options[frm.tid.selectedIndex].value!=''){
        previewframe.window.location.replace("set_template_preview.asp?ID=" + frm.tid.options[frm.tid.selectedIndex].value);
    }
    return false;
} 
</script>  
  
<form action="event_add.asp" method="get" name="frm">

<select style="width: 300px;" name="tid" size="5"><option value="1">Sample Template</select>            
<input type="Submit" value="Apply Template" />
<input style="width: 150px;" type="Button" value="Preview Template" onclick="return PreviewItem()" />

</form>		

<iframe width="450" style="border: 2px;border-color: black;" height="205" name="previewframe" src="set_template_preview.asp" align="left" frameborder="0"></iframe></td>

It only gives the error only when no item is in the select element and the error points to line:

if(frm.tid.options[frm.tid.selectedIndex].value!=''){


&quot;Taxes are the fees we pay for civilized society&quot; G.W.
 
You don't have a default item selected - hence the selectedIndex value contains "-1". You need to perform a check to see if the selectedIndex is equal to or greater than 0 (or preselect one of the options):
Code:
...if(frm.tid.selectedIndex >= 0) {
// Now perform the code check... rest of code goes here
}...

Hope this helps.

Pete.


Web Developer &amp; Aptrix / Lotus Workplace Web Content Management (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Aha, didn't realise it was a >1 sized select box. The selectedIndex will be -1 if nothing is selected. Try this:

function PreviewItem(){
if(frm.tid.selectedIndex>=0){
previewframe.window.location.replace("set_template_preview.asp?ID=" + frm.tid.options[frm.tid.selectedIndex].value);
}
return false;
}

ASCII silly question, get a silly ANSI
 
Thanks guys, exactly what I was looking for.
Much appreciated.

&quot;Taxes are the fees we pay for civilized society&quot; G.W.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top