简体   繁体   中英

javascript change onblur value

I have 2 selections and a textbox. I want to change the onBlur value of the textbox when i select one of the selections. Here is my code:

function changeBlurValue(){

    $selection = document.getElementById("searchtype") 
    $onblurvalue= document.getElementById("searchbox") 
    if ($selection.value="location"){
    $onblurvalue.value="Enter a location name";
    };
}

//html

<select name= "searchtype" id="searchtype" onclick="changeBlurValue()">
               <option value="gymname">Gym Name 
               <option value="location">Location
             </select><br><br>
<input name="searchterm" type="text" size="39" style="color:#888;" id="searchbox"
    value="Enter a gym name" onfocus="inputFocus(this)" onblur="inputBlur(this)" />

The problem is that the selection is permanent at "Location" and I can't change it to "Gym Name"

Thanks for the help.

I would try using onChange instead of onClick for your select.

<select name= "searchtype" id="searchtype" onchange="changeBlurValue()">

Also close your <option> tags, as such:

<select name= "searchtype" id="searchtype" onchange="changeBlurValue()">
  <option value="gymname">Gym Name</option>
  <option value="location">Location</option>
</select>

The problem lies here:

 if ($selection.value="location"){

You should change it to

 if ($selection.value=="location"){

because you are making a comparision.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM