7

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" ] !

2
  • 2
    where most of the validation is done by JS. If it's critical validation, you should also do the validations @ the server, really.
    – KooiInc
    Jun 5, 2011 at 14:17
  • Yup, with out a doubt you are right ! but it's no offense learning the JS hack :)
    – Sourav
    Jun 5, 2011 at 14:19

1 Answer 1

32

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.

7
  • i know that man, but how do you execute JS from client side on a page served by server ?
    – Sourav
    Jun 5, 2011 at 14:30
  • You want to execute Javascript on the server-side? While that is possible, you are better off using PHP, Ruby, Java or some other language focused on the server-side. Javascript focus is on the client-side, i.e., it runs on your browser.
    – faken
    Jun 5, 2011 at 14:38
  • You got me wrong man, i'm looking for something like a SCRIPT WHICH CAN ADD A SIGNATURE TO COMMENT IN SO
    – Sourav
    Jun 5, 2011 at 14:40
  • I'm sorry, you have to be more specific. Javascript runs in your browser, if you want to make permanent changes to a document served by a server, there must also exist a server-side script to accept and perform those changes; those changes can at best be requested by javascript on the client side (using a technique called Ajax), but must be performed by a server-side script.
    – faken
    Jun 5, 2011 at 14:44
  • can you write and run a JS which can change/set a text in the search field of StackOverflow's page ? search field is in the top right corner
    – Sourav
    Jun 5, 2011 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.