Script to change value of a text - Javascript

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

From this question i came to know text element's value can be changed by JS Set maximum number of item in Select List - html

can anyone give some code or some tips ?

My intention is not hacking, i need to know this, coz i'm writing a web app where most of the validation is done by JS

Edit
Looking for guide on running JS from client side on a page served by a server [on some text where it's readonly="true" ] !

回答1:

For example, if you have a html text element like this:

<p id="textelement">I am a text element</p> 

You can change the text inside with JS like this:

<script type="text/javascript">     document.getElementById("textelement").innerHTML = "New text inside the text element!"; </script> 

You can use this technique with any HTML element which can contain text, such as options in a select list (<option> tag). You can select elements in other ways:

  • getElementById() Accesses the first element with the specified id
  • getElementsByName() Accesses all elements with a specified name
  • getElementsByTagName() Accesses all elements with a specified tagname

More info here.

PS - If you want to change the value of an element's attribute, and not its inner text, you should use the setAttribute() method; for example, if you have:

... <option id="optionone" value="red">Nice color</option> ... 

and want to change the value attribute, you should do:

<script type="text/javascript">     document.getElementById("optionone").setAttribute("value", "green"); </script> 

More about this here.



回答2:

Because it's easy to hack client side (JS) validation, I would strongly advise to do/repeat your validation server side. That being said: a text input field has attribute maxlength you can use. Mind you, a textarea has no such attribute. maxlength can be hacked easy using firebug or some other browser helper.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!