简体   繁体   中英

Press enter and show another paragraph

How to make a javascript coding, when I press "ENTER" too the input type="text" , it will show another paragraph p .

 var btn = document.getElementById("key"); btn.onkeypress = function(e){ if(e.keyCode === 13){ e.preventDefault(); } } 
 <input type="text" id="key" placeholder="south"> 

Working fiddle .

It will be better to use a class hidden for example to hide/show your element :

 var btn = document.getElementById("key"); btn.onkeypress = function(e){ if(e.keyCode === 13){ e.preventDefault(); document.getElementById("my-paragraph").classList.remove("hidden"); } } 
 .hidden{ display: none; } 
 <input type="text" id="key" placeholder="south"> <p id="my-paragraph" class='hidden'>Hidden paragraph</p> 


But you could also use inline-style display to show/hide elements, check basic example below.

Hope this helps.

 var btn = document.getElementById("key"); btn.onkeypress = function(e){ if(e.keyCode === 13){ e.preventDefault(); document.getElementById("my-paragraph").style.display='block'; } } 
 <input type="text" id="key" placeholder="south"> <p id="my-paragraph" style='display:none'>Hidden paragraph</p> 

You can create dynamically paragraph and add the text inside it:

 var btn = document.getElementById("key"); var div = document.getElementById("paragraphs"); btn.onkeypress = function(e) { if (e.keyCode === 13) { var newParagraph = document.createElement('p'); newParagraph.textContent = btn.value; div.appendChild(newParagraph); } } 
 <input type="text" id="key" placeholder="south"> <div id="paragraphs"> </div> 

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