Skip to content Skip to sidebar Skip to footer

How To Get Value Of Input Tag In Javasctipt

This input tag always gives null value, what is problem with this code Input tag (HTML)::
) // var title = document.getElementById("myInput").value; // <-- value is not set form.addEventListener('submit', function(e){ var title = document.getElementById("myInput").value; // <-- value is set e.preventDefault(); console.log(title) })
<formclass="mb-3"id='form-wrapper'><inputtype="text"placeholder="Try..."id="myInput"><inputtype="submit"></form>

P.S. I assume you want the input inside the form.

Solution 2:

I assume you want to get the value entered in the form on submitting.

You can add a name attribute to yourinput tag like so:<input name="title" id="my_input"> then in the javascript file you can write this:

$('form').submit(function(e)){
   e.preventDefault();
   let formdata = $(this).serialze();
   console.log(formdata.title);

}

Wrap the input with the form tag...your inputs must be between the form tag. The line let formdata = $(this).serialze(); takes the values with the name atttribute and stores it in the variable formdata

Post a Comment for "How To Get Value Of Input Tag In Javasctipt"