0

I'm trying to detach click event from several p elements via plain JavaScript. I'm able to access these p elements, but for some reason I'm not able to remove the click listener. It works fine with jQuery, but not with pure JS. getP_element function is called upon page load. Here's my code:

function getP_element(){
    console.log("page loaded");
    var p_array = document.getElementById("checkboxesContainer").getElementsByTagName("p");
                for(var i=0;i<p_array.length;i++){
                    p_array[i].onmousedown = new function(){
                        return false; //this doesnt work
                    }
                }
    $("#checkboxesContainer p").click(false); //this works
}

EDIT: More info about what's happening here. I created several custom checkboxes with custom style. Fore some reason the checkboxes get selected even when the user clicks on the p tags, so I figured I need to detach the click events. This is how they are defined inside my HTML:

<div id="checkBoxDiv2" class="checkBoxDiv">
                <input type='checkbox' value='someValue' id="checkBox2"/>
                <label for="checkBox2"><p class="checkBoxText">some text goes here</p>
                    <span></span>
                </label>
            </div>
5
  • 1
    And how are these click handlers added in the first place ? Commented May 21, 2015 at 12:50
  • 1
    Why are you defining a function inside a loop, and why are you using new? Commented May 21, 2015 at 12:51
  • Thanks for the answers, I have added more info to my post. Commented May 21, 2015 at 12:59
  • I'm slightly worried about the future of web development if you are a CS grad working as a front end developer, and you're coding like this :S Commented May 21, 2015 at 13:08
  • The checkboxes are supposed to get checked when clicking the label, if you don't want that, just remove the for attribute ? Commented May 21, 2015 at 14:16

2 Answers 2

1

You don't need to disable the click event.

The checkboxes are getting selected when you click on the p because you have the p tag inside a label which has for="checkBox2"

That's what it's meant to be doing.

Remove the for and it will prevent clicking the label from activating the correspinding input element

Sign up to request clarification or add additional context in comments.

1 Comment

When I remove the for the checkboxes dont check on click.
0

Try

p_array[i].onmousedown = null;

See How to Clear/Remove JavaScript Event Handler?

Edit

The reason that the checkboxes are checked when clicking on p tags has nothing to do with click handlers.

Rather the reason is the for attribute in the parent label tag. This will check the checkboxes when a click occurs on the label.

Change your HTML to

<label><p class="checkBoxText">some text goes here</p>
                    <span></span>
                </label>

5 Comments

Thanks for the answer, but that doesnt work. I have added more info to my post.
Without the for attribute the checkboxes are not getting checked, any suggestions?
@undroid, even if you click the checkbox itself?
Yes, nothing happens.
@undroid, can you reproduce it in a fiddle?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.