Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript/html show alert wich includes label input

Im using HTML5 ad Javascript

I want to be able to input any word into a label and when you click on the button it gives an alert wich includes the given text.

I cannot get it to work with the text I put into the label the alert shows no text.
Ive only found how to do it with predefined labels.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here’s my current html code

function getinput() {
  var input = document.getElementById("form-scream").innerText;
  alert(input);
};
<div>
  <p>Word
    <label id="form-scream"></label>

    <input type="text" name="screaming" id="form-scream">

    <button onclick="getinput()"> Click to scream</button>
  </p>
  <script src="assets/js/scream.js"></script>
</div>

>Solution :

input element does not have innerText property, use value instead. Also, you have use for attribute in a label to associate an element (the element’s id as the value of for):

function getinput()
{
  var inputVal = document.getElementById("form-scream").value;
  alert(inputVal);
};
<div>
    <p>Word
    <label for="form-scream"></label>

    <input type="text" name="screaming" id="form-scream">

    <button onclick="getinput()"> Click to scream</button>
    </p>
    <script src="assets/js/scream.js"></script>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading