Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to check whether a particular element has been clicked?

Maybe my question sounds a bit generic, so let me be more clear.

I have a textarea that is hidden on blur, to be replaced by another div. To be more specific, the above textarea contains markdown code, and the div contains the parsed version of this code.

Now, I am trying to add a formatting toolbar on top of the textarea, but of course, when I click on that, the blur event is triggered and the textarea is hidden, which is not convenient at all.

So my question is whether it is possible at all to prevent the blur default behaviour only when the toolbar is clicked, and if so what's the best way to do that.

like image 495
finferflu Avatar asked Dec 04 '25 00:12

finferflu


1 Answers

Edit: My previous solutions didn't cut it so let's try with something else. I'm not really fond in binding the document click event but in this case it might be the best solution since it seems that you are coding some kind of WYSIWYG-editor. You can possibly tweak it to use .one instead.

$(document).click(function(e){
    if ($(e.target).closest("#container").length == 0) {
        // Target that was clicked is not inside the container, so we can hide or replace the textarea
        $("textarea").hide();
    }
});

You can try this out on jsFiddle.

like image 193
mekwall Avatar answered Dec 06 '25 14:12

mekwall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!