Jump to content

a href in side the document.write method


zeidhaddadin

Recommended Posts

Hi all, How can I do the following:I have an array, each element of this array contains a string which is a site URL such as:myarray[0] = "http://www.google.com"so how can I print it (the whole string) and make it a link to the url it contains, I have got this but it is not working:

document.write("<a href=\"urlsarray[x]\">" + urlsarray[x] + "</a>" + "<br />")

Thanks in advance,zeid

Link to comment
Share on other sites

I am new to JavaScript but I am assuming that you are using something to the effect of:

for (x in urlsarray) {document.write("<a href=\"urlsarray[x]\">" + urlsarray[x] + "</a>" + "<br />")// other code}

The problem lies in the first part of your statement I believe. Each time the for iterates, it writes this to the HTML document

<a href="urlsarray[x]">http://www.google.com</a><br />

You need to separat the "<a href=\"urlsarray[x]\">" into <a href=\"" + urlsarray[x] + "\""

"document.write("<a href=\"" + urlsarray[x] + "\">" + urlsarray[x] + "</a>" + "<br />")

This should make your code work (I think).Try it and see what happens.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...