1

I have an icon on a page, what when clicked is calling a function from an external JS file.

<input type="checkbox" checked="checked" id="myCheck"/>

<img src="icon.png" id="myIcon" onclick="myFunction)(this,null,null)" />

I need to add a functionality to disable this click when a checkbox is checked, and allow click and calling the JS function if the checkbox is not selected.

Tried this but it does not seem to work. What am I missing?

$("#myCheck").prop("checked", true).click(function(event){
  event.stopPropagation();
});

UPDATE: since I do not have much control over code that is not in the header in this particular case, and can not remove onclick="myFunction)(this,null,null) from img tag, is there still a way to disable click action if the checkbox is selected. Perhaps I can remove HTML. What would JS be considered here? It's not an attribute...

1
  • 1
    that binds a click event to the check box, and stops the checkbox's event from propagating. Commented Jan 3, 2012 at 19:41

4 Answers 4

1

Hows about:

HTML

<input type="checkbox" checked="checked" id="myCheck"/>
<img src="icon.png" id="myIcon" />

jQuery

$(function() {
    function iconClick() {
        alert('Clicked Icon!');
    }

    $('#myIcon').click(iconClick);

    $("#myCheck").change(function(e) {
        if (this.checked) $('#myIcon').click(iconClick);
        else $('#myIcon').unbind('click');
    });
});

You can see it working at this jsFiddle.

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

4 Comments

These are all great solutions, however I can not remove onclick="myFunction)(this,null,null) from IMG tag. Is there still a way to disable that JS onclick? UPDATED my question.
@santa then you would have to do something like T. Stone's solution
But again, that would require moving function call from img to the header... won't it?
@santa no it would require modifying myFunction()
1

Is it possible to, instead of disabling the click event, to simply check for the value of the checkbox before proceeding with the myFunction code?

function myFunction() {
    if (!document.getElementById('myCheck').checked) {
        // ...
    }
}

Edit/Disclaimer: Technically the above example code doesn't provide a good example of separation of concerns because it gives knowledge to the myFunction code about the existence of myCheck. Depending on your use case this may or may not be a good design pattern. Ktash's answer provides an alternate solution that while it uses more code is theoretically better separated (though also requiring jQuery).

Comments

0

Instead of doing an onclick attribute, do the following in JavaScript

$('#myIcon').click(function (e) {
    if($('#myCheck').is(':checked')) myFunction(this,null,null);
});

1 Comment

@RoyiNamir Oops, yeah, @Chad got it for me (thanks), but I meant $('#myIcon'). I spend too much time switching between prototype and jQuery lol
0
<input type="checkbox" checked="checked" id="myCheck"/>

<img src="icon.png" id="myIcon" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
(function ($) {
    var checkbox = $('#myCheck'),
        icon = $('#myIcon'),
        fireEvent,
        myFunction;

    fireEvent = function() {
        if(checkbox.is(':checked')){
            icon.off('click');
        } else {
            icon.on('click', myFunction);
        }
    };

    myFunction = function() {
        console.log('I just was clicked!');
    };
    $(function() {
        fireEvent();
        checkbox.on('click', fireEvent);
    });
})(jQuery);

</script>

Comments

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.