简体   繁体   中英

How do I grab a value from an input in javascript?

how do i grab a value from an input element in javascript? For some reason my code is not executing. This is my HTML code:

   <section id ="my-life" style="margin-left:39em;">

   <input class="jenny" placeholder="Your First Name"/> <br /><br />

   <button id="get">Subscribe</button>

   <br /><br />

  <div id ="whitelion">Thank You!</div>



    This is my javascript code:

   function buttonClicked() {
   var Niche = document.getElementsByClassName("jenny");
   var results = document.getElementById("whitelion");


   results.innerHTML = "Hello" + Niche[0].value;

}

 function buttonClicked() { var Niche = document.getElementsByClassName("jenny"); var results = document.getElementById("whitelion"); results.innerHTML = "Hello " + Niche[0].value; } 
 <input class="jenny" placeholder="Your First Name"/> <br /><br /> <button onclick='buttonClicked();'>Subscribe</button> <br /><br /> <div id ="whitelion">Thank You!</div> 

  <section id ="my-life" style="margin-left:39em;">

   <input class="jenny" placeholder="Your First Name"/> <br /><br />

   <button id="get">Subscribe</button>

   <br /><br />

  <div id ="whitelion">Thank You!</div>



<script>
   function buttonClicked() {
     var Niche = document.getElementsByClassName("jenny");
     var results = document.getElementById("whitelion");

     results.innerHTML = "Hello" + Niche[0].value;
   }

  document.getElementById('get').onclick = buttonClicked;

</script>

I tried your code on the editor. It works fine. But you must include your JS code inside a <script> element. And put it in the bottom of the document.

 function buttonClicked() { var Niche = document.getElementsByClassName("jenny"); var results = document.getElementById("whitelion"); results.innerHTML = "Hello " + Niche[0].value; } 
 <section id ="my-life"> <input class="jenny" placeholder="Your First Name"/> <br /><br /> <button id="get" onclick="buttonClicked()">Subscribe</button> <br /><br /> <div id ="whitelion">Thank You!</div> <br/> </section> 

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