Put value in a text box (input)

4

I want to put a value in a text box.

function open_popup(date_today){

        document.getElementById('txtstart').value = date_today;
    }

and a html input

<input type=text required name='txtstart' style='width:150px' value=''>

The text box is in a div that is hidden, and is seen when the open_popup () function is executed. I do not know why this does not work.

    
asked by anonymous 22.10.2014 / 17:06

4 answers

3

You are looking for the element by ID ( getElementById ), but the element has no ID ...

You can use this:

document.querySelector("[name='txtstart']").value = date_today;

or give an element ID by adding id="txtstart" to the input HTML.

Example:

function open_popup(date_today) {
    document.querySelector("[name='txtstart']").value = date_today;
}

open_popup('2014-10-14');
alert(document.querySelector("[name='txtstart']").value);
<div style="display:none;">
    <input type="text" required name="txtstart" style="width:150px" value="">
</div>
    
22.10.2014 / 17:14
1

change to:

function open_popup(date_today){

    $("#txtstart").val(date_today);
}

and no input:

<input type=text required name='txtstart' id='txtstart' style='width:150px' value=''>
    
22.10.2014 / 17:13
1

To use document.getElementById('exemplo') you have to pass the ID of the input box, not the name. You can do this:

<input type=text required name='txtstart' id='txtstart' style='width:150px' value=''>
    
22.10.2014 / 17:41
-3

Add this (onsubmit="return valida_form (this)") in your form.

EX:

<form name="index" method="post" action="nextpage.php" id="index" onsubmit="return valida_form(this)">
    
11.01.2017 / 05:35