show input field once drop down is selected in Contact Form 7

11,357

For anyone looking for a simpler solution. In contact form 7, you can simply add inline JavaScript.

JavaScript added to the form builder will be rendered on the front-end as long as you do not add blank lines within the script.

Here is the copy from a CF7 form editor:

<label> Your Name (required)
[text* your-name] </label>

<label> Your Email (required)
[email* your-email] </label>

<label> Your Favorite Color
[select drop-down-menu id:FavoriteColorDropDown "Pink" "Red" "Purple" "Other"] </label>

<label id="EnterFavoriteColorLabel"> Please Specify Your Favorite Color
[text favorite-color] </label>

[submit "Send"]

<script language="javascript" type="text/javascript">
// Hide the favorite-color text field by default
document.getElementById("EnterFavoriteColorLabel").style.display = 'none';
// On every 'Change' of the drop down with the ID "FavoriteColorDropDown" call the displayTextField function
document.getElementById("FavoriteColorDropDown").addEventListener("change", displayTextField);
  function displayTextField() {
    // Get the value of the selected drop down
    var dropDownText = document.getElementById("FavoriteColorDropDown").value;
    // If selected text matches 'Other', display the text field.
    if (dropDownText == "Other") {
      document.getElementById("EnterFavoriteColorLabel").style.display = 'block';
    } else {
      document.getElementById("EnterFavoriteColorLabel").style.display = 'none';
    }
  }
</script>

Hope that helps.

If you are interested in reading more or extending the same for radio buttons, I recently published an article with more code samples and examples here.

Share:
11,357

Related videos on Youtube

Bilal Naseer
Author by

Bilal Naseer

Founder of https://websensepro.com &lt;--! Helping world grow their &lt;strong&gt;REVENUES&lt;/strong&gt; through Web and Digital channels, coder by hobby never attended college ever in my life :) --&gt;

Updated on June 04, 2022

Comments

  • Bilal Naseer
    Bilal Naseer almost 2 years

    Hi guys I am using Wordpress Contact Form 7 plugin and trying to achieve: Show text field if drop down "Other" is selected

    Drop down menu items

    I am using following raw JS for now through Visual composer in Wordpress Page after adding dropdown as id in Contact Form 7:

    var x = document.getElementById("dropdown");
    if(x.value = "Other") {
     alert('Enter your js here!');
    }