www.webdeveloper.com
Results 1 to 6 of 6

Thread: Prefill field value and readonly

Hybrid View

  1. #1
    Join Date
    Mar 2011
    Posts
    39

    Prefill field value and readonly

    hello Javascripts Guru

    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.

    Someone can help please ??

    thank in advance

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,255

    Question

    Have you tried:
    Code:
    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);
    }

  3. #3
    Join Date
    Mar 2011
    Posts
    39
    many thank for your suggestion.

    I finally found another solution:

    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;
    }</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>
    cheers

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,255

    Question

    Quote Originally Posted by dotcom22 View Post
    many thank for your suggestion.

    I finally found another solution:

    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;
    }</script>
    How does that differ from:
    Code:
    if (event=='reserve') {
      document.getElementById('city').value="New York";
      document.getElementById('city').readOnly=true;
    }

  5. #5
    Join Date
    Mar 2011
    Posts
    39
    yes you right.... This seem to be another syntax possibility...

    After reflection I prefer your solution because the code is more clear..

    thank

    PS: Please take note I did not received any email notification when posted your reply. I looked in my spam box but I found nothing.

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,255
    I did not send any email notification.

    If that is a setting in this forum, I have never noticed it.

    Anyway, good luck!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles