How can I split an HTML element?

I would like a JavaScript to display a DIV element if the HTML form input has a value?

  • I would like a JavaScript to display a DIV element if the HTML form input has a value. If there is no value the div is not displayed. I don't want it to display even if the text in the form input is erased. Thank you in advance.

  • Answer:

    You could make a function called showHideDiv(value) or something similar. It'll check if the input-value is empty or not, and will hide the div-element if it is, and display it otherwise. Then you could add the following attribute to the input-tag: onkeyup="showHideDiv(this.value)" By doing that, you'll call the function whenever you release a key. (Backspace is also a key.) Here's an example: <html> <body> <input type="text" onkeyup="showHideDiv(this.value)"/> <script> function showHideDiv(value) { var somediv = document.getElementById('somediv'); if(value) somediv.style.display = ''; else somediv.style.display = 'none'; } </script> <div id="somediv" style="display:none;"> <h1> HELLO </h1> </div> </body> </html> ————————————— Feel free to add additional details to your question if you need any more help.

Cifru at Yahoo! Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.