1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to override onclick="return false"

Discussion in 'JavaScript' started by Devtard, Jul 9, 2012.

  1. #1
    Hey guys. I need to allow users to check a checkbox after they click on another element (the "parent" checkbox). I tried to use onclick="return true" but it does not work. Can you help me please? Thanks! :)

    Screenshots:

    The parent checkbox is not checked, the child checkbox is readonly.
    canvas.png

    The parent checkbox is checked, the child checkbox is not readonly but it cannot be checked.
    canvas2.png

    Code:
    
    <script type="text/javascript"> 
    function ss_verification_checkbox(checkbox, object){ 
        if(checkbox.checked){ 
            object.readOnly = false; 
            object.style.background='#ffffff'; 
            object.style.color='#333333'; 
            object.className='';
            object.onClick='return true';
        } 
        else{ 
            object.readOnly = true; 
            object.style.background='#f0edea'; 
            object.style.color='gray';
            object.onClick='return false';
        } 
    } 
    </script>
    
    
    
    <p><label><input type="checkbox" name="ss_human_verification_cqa_checkbox" <?php if(get_option('ss_human_verification_cqa') == 1) echo 'checked="checked"'; ?> onClick="ss_verification_checkbox(this, this.form.ss_human_verification_cqa_answer_case_sensitivity_checkbox);ss_verification_checkbox(this, this.form.ss_human_verification_cqa_question_input);ss_verification_checkbox(this, this.form.ss_human_verification_cqa_answer_input)"> <strong>Custom question & answer</strong> - users have to fill an answer to a question</label> 
    
    
    <br><label class="ss_sub_option"><input type="checkbox" name="ss_human_verification_cqa_answer_case_sensitivity_checkbox" id="ss_human_verification_cqa_answer_case_sensitivity_checkbox" <?php if(get_option('ss_human_verification_cqa_answer_case_sensitivity') == 1) echo 'checked="checked"'; if(get_option('ss_human_verification_cqa') == 0) echo ' readonly="readonly" onClick="return false"'; ?>> Force answer check to be case sensitive</label></p>
    
    
    
    
    HTML:

     
    Devtard, Jul 9, 2012 IP
  2. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Have you tried the 'disabled' attribute
    
    object.disabled = false;
    
    Code (markup):
     
    Unni krishnan, Jul 9, 2012 IP
  3. Devtard

    Devtard Notable Member

    Messages:
    850
    Likes Received:
    133
    Best Answers:
    4
    Trophy Points:
    220
    #3
    I tried but it does not work. Any idea why?
     
    Devtard, Jul 10, 2012 IP
  4. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #4
    Did you check error console? I got errors ... this.form is null - you have no forms as I see your code.

    Regards :)
     
    koko5, Jul 10, 2012 IP
  5. Devtard

    Devtard Notable Member

    Messages:
    850
    Likes Received:
    133
    Best Answers:
    4
    Trophy Points:
    220
    #5
    Well, I did not include here the full code, it would be really long. My JS console does not return any errors.
     
    Devtard, Jul 10, 2012 IP
  6. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #6
    My guess would be that, the method is being called twice (event bubbling).
    Can you try object.stopPropagation ( );
     
    Unni krishnan, Jul 10, 2012 IP
  7. Devtard

    Devtard Notable Member

    Messages:
    850
    Likes Received:
    133
    Best Answers:
    4
    Trophy Points:
    220
    #7
    Not working, I still cannot check the checkbox.
     
    Devtard, Jul 11, 2012 IP
  8. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #8
    I hope u have applied the 'stopPropagation' method on the checkBox..... not on the object Parameter.
     
    Unni krishnan, Jul 11, 2012 IP
  9. Devtard

    Devtard Notable Member

    Messages:
    850
    Likes Received:
    133
    Best Answers:
    4
    Trophy Points:
    220
    #9
    I added it here:

    
    <script type="text/javascript"> 
    function ss_verification_checkbox(checkbox, object){ 
        if(checkbox.checked){ 
            object.readOnly = false; 
            object.style.background='#ffffff'; 
            object.style.color='#333333'; 
            object.className='';
            object.onClick='return true';
    [B]       object.stopPropagation();[/B]
         } 
        else{ 
            object.readOnly = true; 
            object.style.background='#f0edea'; 
            object.style.color='gray';
            object.onClick='return false';
        } 
    } 
    </script>
    
    Code (markup):
    Is it the right place?
     
    Devtard, Jul 11, 2012 IP
  10. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #10
    No, It should be checkbox.stopPropagation();
     
    Unni krishnan, Jul 11, 2012 IP
  11. Devtard

    Devtard Notable Member

    Messages:
    850
    Likes Received:
    133
    Best Answers:
    4
    Trophy Points:
    220
    #11
    Changed but it does not affect the checkbox.
     
    Devtard, Jul 11, 2012 IP