Forum Moderators: open

Message Too Old, No Replies

set attribute value from drop down

Need to populate text input from a dropdown

         

alexelisenko

11:22 pm on Aug 2, 2010 (gmt 0)

10+ Year Member



Hello,

I am trying to variably populate the value of a search field depending on the value of a drop down menu.

here is what I have setup, but it works strangely at best.

my form:



<form action="search_process.php" method="post">

<input id="search" style="color:#999999;" name="search" value="Search Title..." onfocus="stext()" onblur="stext()" type="text" maxlength="48" size="48" />

<select name="type" onchange="stext(this.form.type.options[this.form.type.selectedIndex].value)">

<option value="Title">Title</option>
<option value="Tags">Tags</option>
<option value="Actor">Actor</option>

</select>

<input id="submit" name="submit" type="submit" value="Find it!" />




and here is my JS:




<script type="text/javascript">

function stext(value){

var text = document.getElementById("search").getAttribute("value");

if(value === undefined){

var searchtext = 'Search Title...';

}
else if(value == 'Tags'){

var searchtext = 'Search Tags...';

}
else if(value == 'Actor'){

var searchtext = 'Search Actor...';

}
else if(value == 'Title'){

var searchtext = 'Search Title...';

}


if(text==searchtext){

document.getElementById('search').setAttribute("value","");
document.getElementById('search').setAttribute("style","");

}
else if(text==='' || text===' '){

document.getElementById('search').setAttribute("style","color:#999999;");
document.getElementById('search').setAttribute("value",searchtext);

}

}
</script>



This is supposed to clear the searchbox text onFocus, and put some text back if nothing was entered, aswell as change what the default text is depending on the drop down menu. Only as of now, it clears the text and replaces it until the drop down is changed and it does nothing after that.

Any ideas would be helpful thanks.

alexelisenko

11:39 pm on Aug 2, 2010 (gmt 0)

10+ Year Member



I has figured it out. Sorry for wasting a post :).

I have split the two functions, one function for clearing the text and replacing it and another to just set the text on dropdown change.

I can copy the code here if anyone wants it.

Thanks.