views:

116

answers:

4

I'm having a hard time describing what I'm looking for.

If we pretend that we're pulling an array (I've used the .split to get user input data) where each line represents a link.

How can I then add an anchor tagg to that link that I'm pulling?

I need to be able to put

< a href=" + thearray + ">anything< /a>.

The reason for this is that I'm dynamically creating a list.

I think that if I create two variables, one with this part

< a href="

one with the closing and then call some sort of function that puts those two and the pulled array in between them until the list is complete.

Does this make any sense?

edit: here's a link to the full code: http://hem.bredband.net/noor/thecode.txt

+2  A: 

I think you mean this:

for(var x=0;x<thearray.length;x++) {
   document.write '<a href=" + thearray[x] + ">anything</a>'
}

You just want to loop through the array elements, wrapping them in some HTML.

Diodeus
I think this might be it but i'm just to dumb to figure it out, this is where it should be put in:var trt = document.createElement("div"); trt.setAttribute("Id","slideshow1"); trt.className="pics";for(var i=0;x<lines.length;i++) {trt.innerHTML = "<img src=' + lines[i] + '></img>";}this isn't working, is there a book i can read maybe to fully understand what i'm doing? thanks!
Noor
@Noor, inside your for loop you are *replacing* the innerHTML each time, not adding to it. You should use "+=" rather than "=".
tloflin
I've used this now and i'm getting this output:file:///C:/Users/Admin/Pictures/bild3.jpgC:/Users/Admin/Pictures/micke.jpgthe only problem right now is instead of creating 2 different <img src=.... it's creating 1 img element with both of my links as the source.I'll edit the thread with a link to the full code
Noor
+1  A: 

Do you mean that you want to have an array like

["http://www.google.com", "http://www.yahoo.com", "http://www.stackoverflow.com"]

and you want to turn it into

"<a href='http://www.google.com'&gt;anything&lt;/a&gt;
<a href='http://www.yahoo.com'&gt;anything&lt;/a&gt;
<a href='http://www.stackoverflow.com'&gt;anything&lt;/a&gt;"

?

If so, you can just do

var myArray = ["http://www.google.com", "http://www.yahoo.com", "http://www.stackoverflow.com"];
var result = "";
for (var i=0; i<myArray.length; i++) {
   result += "<a href='" + myArray[i] + "'>anything</a>";
}

If not, thinking about "I want to start with X and end up with Y", with specific examples, might help you clarify your question.

JacobM
This is what i've done now but i can't seem to get it right, i updated the thread with a link to what i'm using right now.. its not doing it ..
Noor
A: 

Perhaps you mean something like this:

var tagStart = '<a href="',
    tagEnd = '">anything</a>',
    html = tagStart + thearray.join(tagEnd + tagStart) + tagEnd;

I would still suggest using a loop, though, since the code above will be unkind if thearray is empty.

awgy
A: 

I think using map and then join is going to be more readable:

function makeLink(url)
{
   return "<a href=\"" + url + "\">anything</a>";
}
result = myArray.map(makeLink).join("\n");

More info about map is available at http://www.tutorialspoint.com/javascript/array_map.htm

clahey