Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Checkbox disabled between two forms

    Hi!
    I have two input fields from who one of them i to upload image and other text field to upload image from url .I want to disable one of this fileds and to stay only one .For js code is helping me one friend but him help was enough and the code have some bugs,like fist fileds as active and them when check one has disable and opposite.I want this nly one field to be disable when the page is load but when checkbox is clicked .Sorry for my english .I hope you understand me.

    Code:
    <script type="text/javascript">
                                                window.onload = function(){
                                           var check = document.getElementById('check1');
                                           var file = document.getElementById('file');
                                           var text = document.getElementById('text');
                                           check . onchange = function(){
                                              if( check.checked ){
                                                  text.setAttribute('disabled', 'disabled');
                                                  file.removeAttribute('disabled');
                                              }
                                              else{
                                                  text.removeAttribute('disabled');
                                                  file.setAttribute('disabled', 'disabled');
                                              }
    
                                           };
                                        };
                                        
                                            </script>
                                        <input id="check1" type="checkbox" name="check" /> From link!!!<br/>
                                        Poster:<input  id="file" type="file" name="poster"  />
                                        
                                        Poster from link:<input  id="text" type="text" name="postlink" /><br/>

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Code:
    window.onload = function(){
         var check = document.getElementById('check1');
         var file = document.getElementById('file');
         var text = document.getElementById('text');
         check.onclick = function(){
              if( this.checked ){
                  text.disabled = true;
                  file.disabled = false;
              }
              else{
                   text.disabled = false;
                   file.disabled = true;
              }
    };
    Shorter version:
    Code:
    window.onload = function(){
         document.getElementById('check1').onclick = function(){
              document.getElementById('text').disabled  = this.checked;
              document.getElementById('file').disabled = !this.checked;
         }
    };

  • Users who have thanked glenngv for this post:

    trunder (11-11-2010)

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yes the same.But this is onclick.I want to be when the page is load and you don't click nothing one of fileds to be disable and then when click checkbox to be active this disable filed and other active to be disable.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    That's what my solution does. It only disables/enables the fields when user clicks the checkbox.

  • #5
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Is there any way to be free click at the beginning.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    What do you mean by free click? Can you elaborate what you exactly need?

    What my code does is it does nothing on the fields on page load. Only when the user checks or unchecks the checkbox, the appropriate fields get enabled or disabled.

  • #7
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Code:
    <html>
    <head>
    <script>
    function disableField(myField)
    {
    myField.disabled = true
    return true
    }
    function enableField(myField)
    {
    myField.disabled = false
    return true
    }
    function onfocusField(myField)
    {
    if (myField.disabled)
    {
    myField.blur()
    return false
    }
    return true;
    }
    // The above functions are generic, the following function is specific to this page
    function toggleFields(myField)
    {
    if (myField.checked)
    {
    disableField(document.forms["example1"].text1)
    disableField(document.forms["example1"].checkbox1)
    disableField(document.forms["example1"].select1)
    }
    else
    {
    enableField(document.forms["example1"].text1)
    enableField(document.forms["example1"].checkbox1)
    enableField(document.forms["example1"].select1)
    }
    document.forms["example1"].text1
    }
    </script>
    </head>
    <body>
    <form name="example1">
    Disable Fields: <input type="checkbox" name="control1" onclick="toggleFields(this)" checked="checked">
    <br>
    Text Field: <input type="text" name="text1" onfocus="return onfocusField(this)">
    <br>
    Check box: <input type="checkbox" name="checkbox1" onfocus="return onfocusField(this)" onclick="return onfocusField(this)">
    <br>
    Select: <select name="select1" onfocus="return onfocusField(this)">
    <option></option>
    </select>
    </form>
    <script>
    disableField(document.forms["example1"].text1)
    disableField(document.forms["example1"].checkbox1)
    disableField(document.forms["example1"].select1)
    </script>
    </body>
    </html>
    This is the final code who i need.Run it, then get out last script and see what I mean.


  •  

    Posting Permissions

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