Marc
Marc

Reputation: 6771

jQuery check if element is editable

Problem

I have an editable div containing a few non-editable divs. If a user clicks somewhere I have to find out if he clicked in an area he can actually edit or not.

Question

Is there a way to check if an element is editable? Something like this:

$('my-element-selector').is(':editable')

(And no, the :editable selector is not working, just an example)

Upvotes: 2

Views: 3645

Answers (4)

artemave
artemave

Reputation: 6926

$('#mydiv').get(0).isContentEditable

https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.isContentEditable

Upvotes: 4

4b0
4b0

Reputation: 22323

Give a specific class for editable div and check for class. For example :

 if ( $( "#DivId").hasClass( "Editable" ) ) {}

Another way :

if ( $( "#DivId" ).is( ".Editable" ) ) {

   }

Upvotes: 0

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67187

As far as i heard, there is no such selector called :editable in core jquery, Try to make use of the attribute selector at this context,

$('my-element-selector').is('[contenteditable="true"]')

DEMO

Upvotes: 3

Sridhar R
Sridhar R

Reputation: 20408

Try this

$( "div" ).click(function() {
if ( $( this ).hasClass( "editable" ) ) {

}
});

OR

if ( $( "#myDiv" ).is( ".editable" ) ) {

}

Upvotes: 1

Related Questions