Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Toggle property contentEditable = "true"

I am using contenteditable="true" in a div

What I want to do it to toggle true and false on this attribute everytime I click on a div.

example:

$( "#mylabel" ).click(function() {
    $('#editablediv').attr('contenteditable','false');
});

I tried:

$( "#mylabel" ).toggle(function() {
    $('#editablediv').attr('contenteditable','false');
});

but that didn't work

How can I do this?

like image 573
Satch3000 Avatar asked Dec 11 '22 06:12

Satch3000


2 Answers

Try this way:

$( "#mylabel" ).click(function() {
    var value = $('#editablediv').attr('contenteditable');

    if (value == 'false') {
        $('#editablediv').attr('contenteditable','true');
    }
    else {
        $('#editablediv').attr('contenteditable','false');
    }
});

Keep me posted, hope this helped

like image 196
Andrei CACIO Avatar answered Dec 29 '22 16:12

Andrei CACIO


Here is shorter than your shortest (Reusability and Character Count):

function editTheElement() {
    var a = "contenteditable";
    $(this).attr(a) === 'true' ? $(this).attr(a,'false') : $(this).attr(a, 'true');
}

or still

function editTheElement(e) {
    var a = "contenteditable";
    e.attr(a) === 'true' ? e.attr(a,'false') : e.attr(a, 'true');
}

or

function editTheElement(e) {
    var a = 'contenteditable';
    var v= e.attr(a);
    e.attr(a,!v);
}

or

function editTheElement(e) {
    var a = 'contenteditable';
    e.attr(a,!e.attr(a));
}

or

function editTheElement(e) {
    e.attr('contenteditable',!e.attr('contenteditable'));
}

JS is fun :)

like image 20
Vishal Kumar Sahu Avatar answered Dec 29 '22 16:12

Vishal Kumar Sahu