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.

John
  • 529
  • 8
  • 20

3 Answers3

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!

Alfonso Jiménez
  • 1,235
  • 1
  • 13
  • 24
  • 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. – John Apr 08 '14 at 07:40
  • 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. – Alfonso Jiménez Apr 08 '14 at 08:30
  • The array is made within the function but it would be a simple array of strings. For example {"Confidence","Value","Price"} – John Apr 08 '14 at 08:49
  • 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? – Alfonso Jiménez Apr 08 '14 at 09:05
  • Yes results div is there on the page. If I change it to "", I see "Test" on the webpage, but it doesn't change when the function runs as it should. – John Apr 08 '14 at 09:20
  • Maybe the code is not executed... Did you replace all the myStringArray references with your array? – Alfonso Jiménez Apr 08 '14 at 09:27
  • Yes I think I did. Thanks for letting me know about jsfiddle. Please check the link "http://jsfiddle.net/uMgz5/1/" It works there but not in my webpage. – John Apr 08 '14 at 09:31
  • It seems you are using Mootools. Did you include the library? Also, if you use google chrome, check its "developer tools". If there is an error, you will see it there. – Alfonso Jiménez Apr 08 '14 at 09:43
  • I used mootools only for "Addevent" function. If we remove that, it runs through pure js too. I used chrome and got error "Uncaught TypeError: Cannot set property 'innerHTML' of null". Not sure what is that but it is close to the code line "document.getElementById('results').innerHTML = result" – John Apr 08 '14 at 10:10
  • Maybe the html is not loaded when the function executes. Put the code inside this function to execute it when the page has loaded completelly: window.addEvent('domready', function() { //Put the code here }); – Alfonso Jiménez Apr 08 '14 at 10:17
  • Thank you Alfonoso. I need this code to run after the string is populated which in turn will happen upon user clicking some other links. So I don't think it will work right away after page load. But I found something interesting. The "results" div is in another div called "dlg" in my page. When I use document.getElementById('dlg').innerHTML = result instead of document.getElementById('results').innerHTML = result, it prints the result at the expense of other things in that div. It is only when I try to put it in results div, it displays nothing. Not sure why. – John Apr 08 '14 at 11:23
  • Try changing "results" to something else. Maybe you are using that id elsewhere. – Alfonso Jiménez Apr 08 '14 at 14:33
  • Thank you Sir. It worked. I was indeed using the id "results" somewhere else. I really appreciate it. – John Apr 08 '14 at 15:47
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]);
}
Arun Kumar
  • 1,607
  • 1
  • 18
  • 33
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);
}
Neel
  • 597
  • 6
  • 19
  • 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. – John Apr 08 '14 at 07:37
  • 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? – Neel Apr 08 '14 at 09:56
  • 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. – John Apr 08 '14 at 10:04
  • 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. – John Apr 08 '14 at 11:15