On InternetExplorer, a contentEditable DIV creates a new paragraph (<p></p>
) each time you press Enter whereas Firefox creates a <br/>
tag.
Is it possible to force IE to insert a <br/>
instead of a new paragraph ?
Here's a solution (uses jQuery). After you click on the 'Change to BR' button, the <br>
tag will be inserted instead of the <p></p>
tag.
Html:
<div id='editable' contentEditable="true">
This is a division that is content editable. You can position the cursor
within the text, move the cursor with the arrow keys, and use the keyboard
to enter or delete text at the cursor position.
</div>
<button type="button" onclick='InsertBR()'>Change to BR</button>
<button type="button" onclick='ViewSource()'>View Div source</button>
Javascript:
function InsertBR()
{
$("#editable").keypress(function(e) {
if (e.which == 13)
{
e.preventDefault();
document.selection.createRange().pasteHTML("<br/>")
}
});
}
function ViewSource()
{
var div = document.getElementById('editable');
alert('div.innerHTML = ' + div.innerHTML);
}
These links helped. Working example here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With