RedGiant
RedGiant

Reputation: 4748

iframe not clickable anymore outside appended content

I got this simple iframe text editor from this thread. I've been trying to make a "reply with quote" function to append a post content with html tags to the editor. But I ran into a problem of getting the iframe body clickable and entering text outside the grey appended div element. Is there any solution to get the body clickable again?

HTML:

<div class='margin'><div class='content'><h4>wreeeeeeeeee dsfsd sdfd  sddfsfdsf</h4></div> <button id='reply'>Quote</button>
<div>
<a id="bold" class="font-bold">B</a>
<a id="italic" class="italic">I</a>
<select id="fonts">
 <option value="Arial">Arial</option>
 <option value="Comic Sans MS">Comic Sans MS</option>
 <option value="Courier New">Courier New</option>
 <option value="Monotype Corsiva">Monotype</option>
 <option value="Tahoma">Tahoma</option>
 <option value="Times">Times</option>
</select>

<br/>
<iframe id="textEditor" style="width:500px; height:170px;">
 </iframe> 
<input type='hidden' name='comments' id='comments' />    

<input id='submit'type='submit' value='submit'/>

Code:

    document.getElementById('textEditor').contentWindow.  document.designMode="on";
    document.getElementById('textEditor').contentWindow.  document.close();
    $("#bold").click(function(){

    if($(this).hasClass("selected"))
    {
        $(this).removeClass("selected");
    }else
    {
        $(this).addClass("selected");
    }
        boldIt();
    });
    $("#italic").click(function(){

    if($(this).hasClass("selected"))
    {
        $(this).removeClass("selected");
    }else
    {
        $(this).addClass("selected");
    }ItalicIt();
    });
    $("#fonts").change(function(){
    changeFont($("#fonts").val());
    });

function boldIt()
    {  
       var edit = document.getElementById("textEditor").contentWindow;
       edit.focus(); 
       edit.document.execCommand("bold", false, ""); 
       edit.focus();
    }

function ItalicIt()
     {  
        var edit = document.getElementById("textEditor").contentWindow;
        edit.focus(); 
        edit.document.execCommand("italic", false, ""); 
        edit.focus();
     }

function changeFont(font)
{
    var edit = document.getElementById("textEditor").contentWindow;
        edit.focus(); 
        edit.document.execCommand("FontName", false, font); 
        edit.focus();
}

$('#textEditor').contents().find('body').css("word-wrap", "break-word");

$('#reply').click(function(){
  var content = $(this).prev().html();
  $("#textEditor").contents().find("body").html('<div style="background:grey;color:#fff;border:1px solid #222">Reply to someone<br>'+content+'</div>');
});

(Pardon me for not converting the example code back to jQuery syntax but I got undefined document error in my attempt)

Upvotes: 0

Views: 280

Answers (1)

guest271314
guest271314

Reputation: 1

Try

$('#textEditor').contents().find('body')
.css("word-wrap", "break-word")
.on("keyup", function(e) {
    var _break = $(e.target);
    if (_break.children().is(".break")) {
      $.noop();
    } else {
    _break.find("div")
    .after("<br class=break />");
    };
});

$('#reply').click(function(){
  var content = $(this).prev().html();
  var frame = $("#textEditor").contents();
    frame.find("body")
   .append('<div style="background:grey;color:#fff;border:1px solid #222">'
                          +'Reply to someone<br>'+content+'</div>');
    frame.find("div")
    .after("<br class=break />");
});

jsfiddle http://jsfiddle.net/guest271314/Kxmaf/568/

Upvotes: 1

Related Questions