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 ?

Member Avatar for diafol

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?

It is all unclear, can you please provide a testcase ?

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.

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.

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

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

Member Avatar for diafol

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

Do you mean print to console?

console.log();

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.

Member Avatar for diafol

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

Thanks for your help, these responces will help :)

Member Avatar for diafol

OK, is this solved?

Yes :)

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.

Member Avatar for diafol

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.