changing the value of a text box with .js

22 views
Skip to first unread message

bill

unread,
Sep 5, 2021, 2:17:52 PMSep 5
to
I know how to change the value of a span or div using:
onClick= document.getElementById("searchkey").value = "type min 3
letters";

but that does not work for an input type=text element.

What is the proper way to do that?

-bill

Jon Ribbens

unread,
Sep 5, 2021, 6:44:53 PMSep 5
to
Did you mean that the other way around? If so you might want the
'textContent' property.

JJ

unread,
Sep 6, 2021, 3:39:31 AMSep 6
to
And make sure correct quoting is used. Use HTML entity and escape sequence
if necessary.

Cause, the provided sample code will never work, regardless of what HTML
tag/element it is on.

Arno Welzel

unread,
Sep 6, 2021, 9:09:50 AMSep 6
to
bill:

> I know how to change the value of a span or div using:
> onClick= document.getElementById("searchkey").value = "type min 3
> letters";

<span> or <div> don't have a value. What are you talking about?

> but that does not work for an input type=text element.
>
> What is the proper way to do that?

Using "value" is totally fine:

<input type="text" id="foobar" name="foobar">

<script>
document.getElementById("foobar").value="test";
</script>

However - where exactly do you use "onClick" and did you define
"searchkey" as *id* and not only as name?


--
Arno Welzel
https://arnowelzel.de

Thomas 'PointedEars' Lahn

unread,
Sep 16, 2021, 6:46:57 PMSep 16
to
JJ wrote:

> On Sun, 5 Sep 2021 22:44:44 -0000 (UTC), Jon Ribbens wrote:
>> On 2021-09-05, bill <wil...@TechServSys.com> wrote:
>>> I know how to change the value of a span or div using:
>>> onClick= document.getElementById("searchkey").value = "type min 3
>>> letters";

“span” or “div” elements do not have a built-in “value” property.

>>> but that does not work for an input type=text element.

*That* should work, though, as the corresponding object *does* have a built-
in value property.

>>> What is the proper way to do that?
>>
>> Did you mean that the other way around? If so you might want the
>> 'textContent' property.
>
> And make sure correct quoting is used. Use HTML entity and escape sequence
> if necessary.
>
> Cause, the provided sample code will never work, regardless of what HTML
> tag/element it is on.

By contrast to the “innerHTML” property, the “textContent” property does not
require any escaping beyond the one required for a syntactically correct
ECMAScript program; indeed, if you HTML-escape the value assigned to it, the
escape sequences will be displayed verbatim.

--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
Reply all
Reply to author
Forward
0 new messages