Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set text based on Form <select> value

Tags:

javascript

I have the following <select> in a form:

<select style="width: 100px; text-align: center;">
  <option value="email">Email</option>
  <option value="telephone">Phone</option>
</select>

The default option is Email so the following <input> is show:

<p class="email_form">Please supply your Email Address</p>
<input onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;" style="width: 270px" type="email" name="email" id="email" value="[email protected]">

However if they choose telephone I want to change the above to ask for a telephone number?

like image 676
CLiown Avatar asked Nov 24 '25 00:11

CLiown


1 Answers

Fastest way would be to give the 'label' paragraph a (more generic) ID, and:

<p id="contact_method">Please supply your Email Address</p>

document.getElementById("selectId").onchange = function() {
    var value = this.options[this.selectedIndex].value;
    var p = document.getElementById("contact_method");
    if(value == 'telephone') {
        p.innerHTML = 'Please supply your phone number';
    } else {
        p.innerHTML = 'Please supply your email address';
    }
}

Try it out here: http://jsfiddle.net/29w2G/

like image 90
karim79 Avatar answered Nov 25 '25 14:11

karim79



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!