0

I have the event onClick=window.location.href = 'some url stored in div.

Div contains images as well as text.

I want to show preview using this div, but want clicking disabled on this div because it is taking to the specified location.

I tried it like this:

("#preview").disabled= true;
("#preview").disabled= 'disabled';
("#preview").children().disabled= true;
("#preview").children().disabled= 'disabled';

But none of this is working in firefox 3.6. Somebody please help to solve this problem.

2
  • 1
    So you've got a "click" handler set up, but you want it to not work -- why not just take away that "onclick" attribute? None of those things you wrote will work because, well, they're all wrong. You can't just disable an element like that; it doesn't really make sense.
    – Pointy
    Commented Feb 28, 2011 at 13:41
  • I want to save the html in db with this inline onclick event. but when I preview it I want it to be disabled. how can I do this?
    – Shwetanka
    Commented Feb 28, 2011 at 13:55

6 Answers 6

3

If you want to disable all the div's controls, you can try adding a transparent div on the div to disable, you gonna make it unclickable, also use fadeTo to create a disable appearance.

try this.

$('#DisableDiv').fadeTo('slow',.6);
$('#DisableDiv').append('<div style="position: absolute;top:0;left:0;width: 100%;height:100%;z-index:2;opacity:0.4;filter: alpha(opacity = 50)"></div>');
3
$('#preview').unbind('click');
1
  • this will not work for an inline-handler.
    – jAndy
    Commented Feb 28, 2011 at 13:50
1

First, why would you use an inline event handler while using a library like jQuery. Do it the unobtrusive way and bind all event handlers with Javascript.

This actually will also help you with your problem, because then you can unbind and rebind an event handler very easily:

function myclick() {
   window.location.href = 'some url';
}

// bind the click event handler
$(function() {
    $('#preview').bind('click', myclick);
});

// unbind the click event handler
$('#preview').unbind('click', myclick);

That way, you can add and remove the functionality, but it won't change any visible change to the div node. You would also have to add a style or css class to let an user know that something changed.

Ref.: .bind(), .unbind()

1
  • Actually it is fixed html code in which I'm just replacing a few things. Kind of a template and this html is saved in db. I want all the clicks and this event to be disabled when preview it in a div. but when I actually use it on the site this event should be there. how to do this?
    – Shwetanka
    Commented Feb 28, 2011 at 14:01
0

You should be able to simply do:

// Unbind all click events for all elements
("#preview *").unbind("click");

The other thing you could do is what modals often do with the background: place a non-clickable div on top of the other content.

0
$("#preview div").onClick = function(){};
0

Try

$('#the_div_id *').unbind('click');

Not the answer you're looking for? Browse other questions tagged or ask your own question.