0

Note: This question is marked as a duplicate but I'm not looking to get the script element but the div element as seen in my code where the script is in. Whereas the "duplicate" is looking to get the script tag. this is not the case with my script.

I am using JavaScript for my current project and was wondering if there is a way to detect which HTML element an object was created in.

Consider the following code.

Editable.js

function Editable(elem, form1, form2) {

    this._edit_form;
    this._value_form;
    this._current_form;

    //Getters
    this.getEditForm        = function() { return this._form1; };
    this.getValueForm       = function() { return this._form2; };
    this.getCurrentForm     = function() { return this._current_form; };

    //Setters
    this.setCurrentForm = function(current_form) { this._current_form = current_form; };

    this.switch = function() {

        if(_current_form == _edit_form)
            _current_form = _value_form;
        else
            _current_form = edit_form;

    };

    var __construct = function() {

        $(this.elem).html(this._value_form);

    }();

}

-

<html>
<head>
    <script src='Editable.js'></script>
</head>
   <body>
      <div>
      <script>new Editable(this, "Company Name", "<input type='text' placeholder='enter a company name' />");</script>
      </div>
   </body>
</html>

The point of an editable is that it has a value form and an edit form. I want to be able to switch between those by removing the text from the element and placing an HTML Input field. This change is supposed to happen when clicking the element.

The problem now is that "this" logs as

Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…}

While the object would need to know what element it is in so it may edit it's contents. So I would need the DIV HTML element for that.

Thank you very much.

0 Answers0