I have a form with several fields and I need to customize some fields using javascript for allow me to prefill a value and make the field "readonly".
For example I have a field "city" where I want prefill the value "New York". So for do that I used this code:
HTML Code:
<script type="text/javascript">function on_form_loaded(event) {
if (event=='reserve')
document.getElementById('city').setAttribute("value", "New York");
}</script>
This work without problem...
Now for make the field "readonly" I used this code:
HTML Code:
<script type="text/javascript">function on_form_loaded(event) {
if (event=='reserve')
document.getElementById('city').readOnly=true;
}</script>
This work without problem too...
Now I use both code this don't work and only related "readonly" code is taken in consideration. The code related to prefill the value "New York" do not work anymore. It seem I must combine the code and is probably possible to shorten the code for get the two desired properties, but I don't know how to do this.
I must precise I'm newbie and don't have any coding knowledges except in html and css.
if (event=='reserve') {
document.getElementById('city').setAttribute("value", "New York");
document.getElementById('city').readOnly=true;
}
Or this:
Code:
if (event=='reserve') {
document.getElementById('city').setAttribute("value", "New York");
document.getElementById('city').setAttribute('readOnly',true);
}
<script type="text/javascript">function on_form_loaded(event) {
if (event=='reserve')
var city = document.getElementById('city');
city.value = "New York";
city.readOnly = true;
}</script>
Using variable was exactly what I need because I want add some more fields. Some of them do not require to have prefilled value but only "readonly" attribute.
Example:
HTML Code:
<script type="text/javascript">function on_form_loaded(event) {
if (event=='reserve')
var city = document.getElementById('city');
city.value = "New York";
city.readOnly = true;
var phone = document.getElementById('phone');
phone.readOnly = true;
}</script>
<script type="text/javascript">function on_form_loaded(event) {
if (event=='reserve')
var city = document.getElementById('city');
city.value = "New York";
city.readOnly = true;
}</script>
How does that differ from:
Code:
if (event=='reserve') {
document.getElementById('city').value="New York";
document.getElementById('city').readOnly=true;
}
Bookmarks