Get the App
SLTechnology News&Howtos  ›  Development  › 

How to set the input box to read-only by javascript

Shulou Source: shulou.com Published: 2022-06-02 07:21:24 09月21日 Update

This article mainly introduces "how to set the input frame to read-only in javascript". In daily operation, I believe many people have doubts about how to set the input box to read-only in javascript. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to set the input frame to read-only in javascript". Next, please follow the editor to study!

Setting method: 1, use the "document.getElementById (id)" statement to get the input element object according to the specified id value; 2, use the "input object .setAttribute (" readOnly ", true)" statement to add a read-only style to the input element.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

In javascript, to set the input box to read-only, simply add a read-only attribute, readOnly, to the input element using the setAttribute () method.

The setAttribute () method adds the specified property and assigns it the specified value.

Syntax:

Element.setAttribute (attributename,attributevalue)

Example:

Function my (id) {return document.getElementById (id);} my ("btn"). Onclick = function () {my ("text"). SetAttribute ("readOnly", true);}

At this point, the study on "how to set the input box to read-only in javascript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Methods learning elements objects attributes more statements help practical next tutorials articles styles environment theory computers knowledge examples articles systems Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn macOS MariaDB Shulou Tech Info Redmi