Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-20-2013, 04:24 AM   PM User | #1
Varemenos
New to the CF scene

 
Join Date: Jun 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Varemenos is an unknown quantity at this point
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:
Quote:
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..
Varemenos is offline   Reply With Quote
Old 02-20-2013, 07:32 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,032
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 02-20-2013, 07:53 AM   PM User | #3
Varemenos
New to the CF scene

 
Join Date: Jun 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Varemenos is an unknown quantity at this point
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.
Varemenos is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:15 PM.


Advertisement
Log in to turn off these ads.