Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to convert this JQuery code to pure JavaScript

    I'm working on an online IDE (similar to jsFiddle, codepen, jsbin etc) and there is an issue I can't solve by myself.

    Code:
    var console={
        panel : $(parent.document.body),
        log : function(m){
            this.panel.find("#target").append("<p>>" + m + "</p>");
        }
    };
    I have JQuery code above which basically re-implements the console object so instead of printing the results in the JavaScript console to output the string in the iframe parent's div.

    I tried converting it myself but I failed.

    Code:
    var console = {
    	panel : parent.document.body,
    	log : function(m) {
    		this.panel.getElementById("target").appendChild(document.createElement("p")); // trying to create a "p" element fails
    	};
    };
    It fails and returns this error:
    Uncaught TypeError: Object [object HTMLBodyElement] has no method 'getElementById'
    Can someone help me fix this?


    edit:
    I just tried to manually run this:
    Code:
    parent.document.getElementById("target").appendChild(document.createElement("p"));

    edit#2:
    Nvm, I managed to fix it by doing this:
    Code:
    var console = {};
    console.log = function(m) {
    	parent.document.getElementById("target").insertAdjacentHTML("beforeend", "<p>> " + m + "</p>");
    };
    Last edited by Varemenos; 02-20-2013 at 04:43 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    You have marked your thread as "Resolved" so I take it that you no longer need a response.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    You have marked your thread as "Resolved" so I take it that you no longer need a response.
    Yes I've managed to fix it,
    it had something to do with the way i was creating the console object.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •