0

Can you write within the script the new order of an array as a comment without the script actually using it ? Suppose I have an array of 10 items, then I push and unshift more items in the array, for myself or anyone else viewing the code I want to show the list in the new order, can this be done ?

5
Contributors
14
Replies
52
Views
3 Years
Discussion Span
Last Post by diafol
0

What like cloning the original array and messing with it while leaving the original intact?

//edit

Ok just re-read

Not sure if I understand. Do you mean like using the console and watching values of variables as you step through a routine?

Edited by diafol

0

I'm slowly getting over a bad cold :(

var list = ["one","two","tree"];
list.unshift("pears","cars");
document.write(list);

I want to write the list as a comment or as a part of the code that won't be executed for personal reference.

0

To concatenate the individual strings together, comma separated :

var list_ = list.join();

Then you can do what you want with list_.

alert(list_);
document.getElementById('myDiv').innerHTML(list_);//pojs
$("#myDiv").html(list_);//jQuery

Only use document.write in the document writing phase. Once the document has achieved its ready state, document.write will blitz it completely.

Edited by Airshow

0

Try this:

HTML

<div id="element-to-be-commented">
    <div>Some div...</div>
    <div>Inspect the element underneath this</div>
</div>

JS

var commentContainer = document.getElementById("element-to-be-commented");

var data = {name: "gon1387", notAnotherArray: [2,4,1]};

// Convert the object to string through JSON api 
// then create a comment out of the result
var convertedData = document.createComment(JSON.stringify(data));

// Put the commented data on the element
commentContainer.appendChild(convertedData);

Inspect the element right after running.
Check my sample in this fiddle: SampleFiddle

0

AirShow - Check this link the array is not printing as a comment within the script, rather it is prompting the list ?

0

What do you mean by "comment within the script"?

Do you mean print to console?

console.log();

Edited by diafol

0

Siberian, those were just three possible things you might want to do with the concatenated list.

I can see no value in creating a synthetic comment node.

0

Ah, so he means create a html comment (<!-- blah -->)

0

Ah, so he means create a html comment (<!-- blah -->)

Presumably Ardav, but goodness knows why. It's not as if "View Source" will magically show the comment.

0

It's not as if "View Source" will magically show the comment.

Exactly, he'll only see it in the 'Elements' option of something like Chrome's Inspect. So, pretty pointless. May as well use the console log

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.