/    Sign up×
Community /Pin to ProfileBookmark

Javascript doesnot work for a dropdown option with no value.

Hi,
My javascript function works fine in IE but not in Firefox . In a form I have a list of drop down options with the default title. the value of the dropdown options is a direct url . If the user selects an option from the dropdown and clicks on the Go button the target page will be displayed . The title doesnot have a value . so if the user doesnot select any option from the list and clicks on the Go button the title will be the default option.Since the value is null the context remains the same.
Please help me to find out a solution.
source code:

<html>
<script language=”Javascript”>
function trim(text)
{
var endIndex = text.length – 1;
var isSpace = true;
var startIndex = 0;
while (isSpace && startIndex < text.length)
{
isSpace = text.charAt(startIndex) == ‘ ‘;
if (isSpace) startIndex++;
}
isSpace = true;
while (isSpace && endIndex >= startIndex)
{
isSpace = text.charAt(endIndex) == ‘ ‘;
if (isSpace) endIndex–;
}
return text.substring(startIndex,endIndex + 1);
}

function buildTarget()
{
var ProcureTarget ;
ProcureTarget =document.form1.chooseLink.options[document.form1.chooseLink.selectedIndex].value ;
ProcureTarget =trim(ProcureTarget);

if(ProcureTarget !=”)
{
window.location.href =ProcureTarget;
}
else
{}
}

</script>
<form name=form1 method=post action=”javascript:buildTarget()”>
<select id=’SearchData’ NAME=’chooseLink’>
<option id=”1″>Popular Links</option>
<option id=”2″ value=’http://www.yahoo.co.in‘ >
Procurement Link1</option>
<option id=”3″ value=’http://www.worldbank.org‘ >Link2</option>
<option id=”4″ value=’http://www.google.com‘ >Link2</option>
</select>
<input type=”button” onClick=”buildTarget()”>Click here</form>
</html>

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@anushyaauthorJul 25.2007 — Hi,

My javascript function works fine in IE but not in Firefox . In a form I have a list of drop down options with the default title. the value of the dropdown options is a direct url . If the user selects an option from the dropdown and clicks on the Go button the target page will be displayed . The title doesnot have a value . so if the user doesnot select any option from the list and clicks on the Go button the title will be the default option.Since the value is null the context remains the same.

Please help me to find out a solution.

source code:

<html>

<script language="Javascript">

function trim(text)

{

var endIndex = text.length - 1;

var isSpace = true;

var startIndex = 0;

while (isSpace && startIndex < text.length)

{

isSpace = text.charAt(startIndex) == ' ';

if (isSpace) startIndex++;

}

isSpace = true;

while (isSpace && endIndex >= startIndex)

{

isSpace = text.charAt(endIndex) == ' ';

if (isSpace) endIndex--;

}

return text.substring(startIndex,endIndex + 1);

}

function buildTarget()

{

var ProcureTarget ;

ProcureTarget =document.form1.chooseLink.options[document.form1.chooseLink.selectedIndex].value ;

ProcureTarget =trim(ProcureTarget);

if(ProcureTarget !='')
{
window.location.href =ProcureTarget;
}
else
{}
}



</script>

<form name=form1 method=post action="javascript:buildTarget()">

<select id='SearchData' NAME='chooseLink'>

<option id="1">Popular Links</option>

<option id="2" value='http://www.yahoo.co.in' >

Procurement Link1</option>

<option id="3" value='http://www.worldbank.org' >Link2</option>

<option id="4" value='http://www.google.com' >Link2</option>

</select>

<input type="button" onClick="buildTarget()">Click here</form>

</html>
Copy linkTweet thisAlerts:
@Orc_ScorcherJul 25.2007 — Actually Fx is right and IE is wrong (how unusual): an option element without a value attribute gets the option's content as its value. In your case here you could simply check whether selectedIndex is 0 or not.
Copy linkTweet thisAlerts:
@anushyaauthorJul 25.2007 — Hi Orc ,thanks a lot. But if any other item in the dropdown doesnot have vlaue then the javascript breaks if that option is selected.Please advise to proceed.

-anushya
Copy linkTweet thisAlerts:
@Orc_ScorcherJul 25.2007 — Try this:function buildTarget() {
var select = document.form1.chooseLink
var option = select.options[select.selectedIndex]
if (option.value != "" &amp;&amp; option.value != option.text)
location.href = trim(option.value)
}
Copy linkTweet thisAlerts:
@FangJul 25.2007 — Test for index. If there is no value Fx will return the text.ProcureTarget =document.form1.chooseLink.options[document.form1.chooseLink.selectedIndex].value ;
[COLOR="Green"]if(!document.form1.chooseLink.selectedIndex){return;}[/COLOR]
×

Success!

Help @anushya spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...