cmp cmp - 1 year ago 63
Javascript Question

Can I add links to a JS array?

I want to have a link to add to each of the elements in the array below:

var breadcrumb = ['About','Test2','Test3','Test4' ];

I tried to add

<a href ="/about/">About</a>

etc and so on but not good.

Is there a way of doing this within the array? And how can I print them to my aspx page?

Thank you

Answer Source

Yes, you can put links or anything into a javascript array (use the real href values for you):

var breadcrumb = [
    "<a href='about.html'>About</a>",
    "<a href='test2.html'>Test2</a>",
    "<a href='test3.html'>Test3</a>",
    "<a href='test4.html'>Test4</a>"

If by printing you mean writing them to the page - I don't think this has anything specific to do with aspx since the array is in Javascript. You could do this:

for( var index = 0; index < breadcrumb.lengh; index++ ){
     document.write("<div>" + breadcrumb[index] + "</div>" );

Or you could use jQuery for more elaborate DOM manipulation to add your links to the page.