Placing value in a text box (input)

Asked

Viewed 61,202 times

6

I want to put a value in a text box.

function open_popup(date_today){

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

and html input

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

The text box is in a hidden div, and is seen when the open_popup() function is executed. I don’t know why this doesn’t work.

4 answers

4


You’re looking for the element by the ID (getElementById), but element does not have ID...

You can use it like this:

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

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

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>

1

change to:

function open_popup(date_today){

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

and in input:

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

1

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

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

-4

Add this (onsubmit="Return valida_form(this)") to your form.

EX:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.