/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] dynamic content? (JS noob)

My JS is limited so be gentle. I want to use a menu/list to alter the content of a page. I was able to retrieve and output the content from DB but I don’t know how to output it in the proper place on the page. I tried using id to identify a table and then getElementById to post the information there, but I don’t think I’m using it correctly, or perhaps there is a better approach all together.
Most of this is done through PHP so disregard any extra garbage (‘ or ;;;, etc.). I tried to delete out the unrelated code.
JS script: (I removed all the PHP db content retrieval, because that is working ok)

[CODE]<script type=”text/javascript” language=”javascript”>
function getphdlist(form) {
//…phpjunk
var phdarray = new Array();
//…phpjunk
phdarray[‘ . $j . ‘] = new Array(//…phpjunk
);
var startyr = 1996;
var curyr = 2005;
var range = curyr – startyr;
var x = 0;
var z = form.phdyear.value;
while (x < range – 1) {
document.getElementById(phd).write(“<tr><td>”+phdarray[z][x][0]+”</td><td>”+phdarray[z][x][1]+”</td></tr>”);
x++;
}
}</script>[/CODE]

Menu:

[CODE]<form name=”phdyrsel” id=”phdyrsel” onChange=”getphdlist(this)”>
<select name=”phdyear” class=”bodytext” id=”phdyear”>
<option class=”bodytext” value=”all” selected>(All)</option>’;

echo ‘<option class=”bodytext” value=”‘ . $i . ‘”>’ . $per . ‘</option>’;

}
</select>
</form>’;[/CODE]

Table:

[CODE]<tr>
<td colspan=”2″><table id=”phd” name=”phd”></table></td></tr>[/CODE]

The error I recieve: document.getElementById(phd).write is not a function. Looking through some of the other posts on this (getElementById) it looks like this method is used for altering attributes (ie document.getElementById.style = …). Is there a way to use this as a reference to a place on the page? I also tried adding document.getElementById to the function call (in the form) but it still didn’t work right. How can I get this content to post in the right place?
I’m still learning JS so feel free to suggest alternate methods other than getElementById.

Thanks in advance. ?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@_Aerospace_Eng_Dec 22.2005 — You can use innerHTML
document.getElementById(phd).innerHTML="&lt;tr&gt;&lt;td&gt;"+phdarray[z][x][0]+"&lt;/td&gt;&lt;td&gt;"+phdarray[z][x][1]+"&lt;/td&gt;&lt;/tr&gt;";

though others may suggest a DOM method. I don't know how to use DOM too well yet.
Copy linkTweet thisAlerts:
@bwalker_lt___gt_authorDec 22.2005 — Perfect! I knew it would be an easy fix for you guys! THANKS!
Copy linkTweet thisAlerts:
@bwalker_lt___gt_authorDec 22.2005 — Now I have another problem. It cycles through the 'while statement' correctly once and then returns the error phdarray[z][x] has no properties. I am incrementing x at the end of the if, but why would this alter the nature of the phdarray variable.
[CODE]while (x < range - 1) {
if (phdarray[z][x][2] == "")
{
document.getElementById('php').innerHTML="<tr><td>"+phdarray[z][x][0]+"</td><td>"+phdarray[z][x][1]+"</td></tr>";
}
else {
document.getElementById('php').innerHTML='<tr><td><a href="'+phdarray[z][x][2]+'"

target="_blank">'+phdarray[z][x][0]+'</a></td><td>'+phdarray[z][x][1]+'</td></tr>');
}
x++;
}[/CODE]


I should mention that when I used document.write (in place of document.getElementById('php').innerHTML) it correctly retrieved and output all of the records in the array without error.

Any suggestions?
×

Success!

Help @bwalker_lt___gt_ spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...