Click here to Skip to main content
15,442,199 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I am checking the event's keyCode for backspace

C#
var element = document.activeElement;
if (!element.isContentEditable){
    if (event.keyCode == 8) {
        return false;
    }


This code works fine with IE, but not with chrome as isContentEditable is always return false. anyone knows the answer, why?
Posted
Comments
Sergey Alexandrovich Kryukov 9-Oct-14 3:46am     CRLF
You don't really need this check. Mozilla and Chrome behave as I described. I did not check up documentation/standards, but usually, in the cases of such differences, IE is doing wrong. —SA
bitsmax 9-Oct-14 4:18am     CRLF
I check it because i want to avoid backspace on any alert/message box . Becouse alert or message box is not editable so i am checking whether the current selection/active element is editable or not. while i am entering username in username text box , the active element is text box , and it should be editable.
Sergey Alexandrovich Kryukov 9-Oct-14 10:58am     CRLF
What elements are you checking up? "alert" and "message box" are not HTML elements. Text box will give you false. You simply need to assume it is always editable, in your sense. The property you are using is just useless for this purpose (it is related to attribute contentEditable, nothing else), forget it. If it is disabled or something (hidden, etc.), check it up. —SA

1 solution

This is because your element is never editable. As simple as that.

You need to know two things here:
http://www.w3schools.com/jsref/prop_html_iscontenteditable.asp[^],
http://www.w3schools.com/jsref/prop_html_contenteditable.asp[^].

Now, some background:

This property has nothing to do with "normal" input control. It is related to the attribute contentEditable which allows you to edit formatted content of something like div or p.

Compare behavior of the div and input control on this example:
XML
<html>
    <head>
        <title>Example</title>
    </head>
<body>

<input type="text" id="text"/>
<div contentEditable="true" id="div">some content</div>

<script>
   text = document.getElementById("text");
   div = document.getElementById("div");
   alert("Editable: text: "
      + text.isContentEditable + "; div: "
      + div.isContentEditable);
</script>

</body>
</html>


First, you will see that you are able to edit the element with the text "some content", even though this is the regular markup. This is how HTML editors are created. You will see that only this element is considered editable.

—SA
 
Share this answer
 
Comments
bitsmax 9-Oct-14 4:23am    
Consider the scenario when your text box with id="text" is active , you are entering some value in your text box , that time "var test = document.activeElement" your textbox with id="text" is a active element. on same element "test" i am checking that is the text box is editable or not by property "test.isContentEditable" . Is that wrong ? Or i have to use property contanteditable="true" everytime ?
Sergey Alexandrovich Kryukov 9-Oct-14 11:02am     CRLF
I already answered in detail and explained how this property behaves. If you want to use this contentEditable="true" (attribute, not property, property is "isContentEditable"), do it, it's up to you. But why? You need to understand one simple thing: this property had nothing to do with the ability to change value of control elements. As simple as that. Enabled and not hidden control elements are always editable. I did not really understand why do you need this check. Active element cannot be non-editable, because only a control (enabled, visible) can be active. —SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900