Show and Hide a textbox using JavaScript and jQuery

Today, we’ll explain to you how to show and hide a textbox using JavaScript and jQuery.

Here, we will show/hide the div with a textbox depending on the selection of the dropdown list. If the user selects yes from the dropdown list then the textbox will appear.

Example

Output - Show and Hide a textbox using JavaScript and jQuery - Clue Mediator
Output – Show and Hide a textbox using JavaScript and jQuery – Clue Mediator

Steps to show and hide a textbox using JavaScript & jQuery

  1. Create an HTML
  2. Add CSS
  3. Using JavaScript show/hide a textbox
  4. Using jQuery show/hide a textbox
  5. Output

1. Create an HTML

First, we will create an HTML where dropdown and textbox are shown. Check the following code.

2. Add CSS

Add the following CSS code before the closing head (</head>) tag for the basic UI style.

3. Using JavaScript show/hide a textbox

Now using the JavaScript, we will hide and show the textbox as per dropdown selection.

When an option is selected from the dropdown, the showHide() function will be executed. If the selected value is 1, the div with id hidden-field will show otherwise it will be hidden.

Add the following code at the end of the HTML file.

4. Using jQuery show/hide a textbox

Similarly, we will do the same task using jQuery. We need to include the jQuery library and the script in the HTML file.

jQuery library

Script

5. Output

Let’s combine all above code together and see how it works.

JavaScript Code
jQuery Code

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *