0

Trying to write a basic jQuery plugin:

HTML:

<textarea>
    <p>Hello World!</p>
</textarea>

jQuery:

$.fn.wysiwyg = function (options) {

    var e = $(this).replaceWith("<iframe>");
    console.log($(this)[0].contentDocument);

};
$("textarea").wysiwyg();

JSFIDDLE DEMONSTRATION

The Problem

var e = $(this).replaceWith("<iframe>");
console.log($(this)[0].contentDocument);

I am getting undefined at the console. If I set an id attribute to this iframe and target that id in console.log method, it works perfectly, but I want to use $(this). What can be done for this?

Faiz Ahmed
  • 1,083
  • 8
  • 16

2 Answers2

0

The problem is this will still refer to the textarea so

$.fn.wysiwyg = function (options) {

    var $frame = $("<iframe>")
    var e = $(this).replaceWith($frame);
    console.log($frame[0].contentDocument);

};

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Try This:

$.fn.wysiwyg = function (options) {

    var $e = $("<iframe>");
    this.replaceWith($e);
    console.log($e);

};


$("textarea").wysiwyg();

:: JsFiddle ::

mk117
  • 753
  • 2
  • 13
  • 26