1

I am very new to web development and under major deadline. I would really appreciate your help on this.

I have an array which gets created through a javascript function which runs when user clicks on a particular table on the webpage.

<tr id="table1_1_1_0">
    <td><a href="#" onclick="func_get_fields('table1_1_1_0');">Primary</a></td>
</tr>

This function func_get_fields creates an array List_of_Screen_Names which has entries to be displayed.

My question is how do I display the elements of this returned array in the webpage so that each of them is a link in itself.

I found some code which works with the php arrays but not with javascript.

How can i do this ?

I tried another approach

document.write('<table border="1" cellspacing="1" cellpadding="5">')

for(i = 0; i < List_of_Screen_Names.length; i++){
   document.write('<tr><td>');
   document.write(List_of_Screen_Names[i]);
   document.write('</td></tr>');
}

document.write('</table>');

This creates a table of strings which I think can be changed to links. But it completely wipes off the webpage and shows only the table. How to make it display within a div.

3 Answers 3

4

You can modify elements inner html with javascript. For example if you want to show the results in <div id="results"></div> the code would be:

var myStringArray = ["http://google.com","http://bing.com"]; //Sample array, use yours
var result = ""
for (var i = 0; i < myStringArray.length; i++) {
    result = result + " <a href='" + myStringArray[i] + "'>"+ myStringArray[i] + "</a>";
}
document.getElementById('results').innerHTML = result

I hope it helps!

Sign up to request clarification or add additional context in comments.

13 Comments

Thank you Alfonso. I tried this with my array( I put semicolons in both lines) but it didn't work. The webpage still doesn't display anything. I am not sure why. It certainly looks like it should work. Do you have any ideas to fix it. Thanks again.
Could you post your array? I tried my code on jsfiddle and it works. By the way, the semicolons at the end of the lines are optional in javascript. I normally write them but I wrote the code above in a hurry.
The array is made within the function but it would be a simple array of strings. For example {"Confidence","Value","Price"}
The problem seems not to be there. Did you add the "results" div to your html? Or, if you chose an element of your own html, did you check that it had the id field?
Yes results div is there on the page. If I change it to "<div id="results"><a href="#">Test</a></div>", I see "Test" on the webpage, but it doesn't change when the function runs as it should.
|
0

using "split" in javascript . it's returns array value

split

function func_get_fields(d_id)
{
    var ar = d_id.split('_');
alert(ar[1]);
}

Comments

0

If List_of_Screen_Names is an array of links:

for(var i=0;i<List_of_Screen_Names.length;i++){
    var newLink = document.createElement('a');
    newLink.innerHTML = List_of_Screen_Names[i];
    document.body.appendChild(newLink);
}

4 Comments

Thanks for your comment. I added about code to the function which creates the array but it didn't work. Do I have to add a div in the html for this which I can style later.
This code should be after the array is filled. Can I see what the contents of your array looks like? Did no links show up at all?
The array is made within the function but it would be a simple array of strings. For example {"Confidence","Value","Price"}. I put the code after the array was filled.
Thank you Neel, it worked. It displayed the my strings from array. But how do I make them display where I want. Do I have to create a div in HTML and style it later on through CSS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.