I'm using this function in order to replace <DIV>New Divs</DIV>
with <BR>
in break lines on contentEditable divs using Safari and Chrome:
$("#form_description").live("keypress", function(e){
if (e.which == 13) {
if (window.getSelection) {
var selection = window.getSelection(),
range = selection.getRangeAt(0),
br = document.createElement("br");
range.deleteContents();
range.insertNode(br);
range.setStartAfter(br);
range.setEndAfter(br);
range.collapse(false);
selection.removeAllRanges();
selection.addRange(range);
return false;
}
}
});
The problem is that when I'm typing inside an <UL><LI>Something</LI></UL>
and I press the "return" key, I don't want to get a <BR>
, so I can continue whit the list...
How could I create a condition for that function not to work if I'm inside a LI
element?
EDIT WITH MY ANSWER:
Ok, I've managed to do it, but If you have any comment on the code, you're welcome:
$("#form_description").live("keypress", function(e){
if (e.which == 13) {
node = document.getSelection().anchorNode;
nodeName = node.parentNode.nodeName.toLowerCase();
if ((nodeName != "li") && (nodeName != "blockquote")) {
... etc }