Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create Next button loop

Status
Not open for further replies.

alehambre

Programmer
May 7, 2006
15
US
I am using DOM to view XML in an HTML form. I need some help constructing a loop to allow user to click "Next" button, to view next XML node. I am trying to use the variable "A" to control the array position. I want to create a loop that adds 1 to the value of A each time the button is clicked.
Please help me understand how to break after 1 is added to "A", and how to continue the loop each time the button is clicked.

PrintNode = xmldoc.childNodes[2].selectNodes('./Print');

var A = 0

function NextNode(){
for ( var A = 0; A < 100; A++)
{
document.getElementById("printid").value = PrintNode[A].childNodes[0].text
}
}


Sample XML:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="Printlist.xsl"?>
<PrintList>
<Print>
<ID>001000</ID>
<PrintName>Spring Garden</PrintName>
<Season>spring</Season>
<PrintSize>5x9</PrintSize>
<file href="springgarden.jpg"/>
<Desc></Desc>
</Print>
</PrintList>
 
Try something like this instead of a loop:
Code:
var A = 0;

function NextNode(){
A++;
if (A < 100)
  {
  document.getElementById("printid").value = PrintNode[A].childNodes[0].text;
  }
}

Lee
 
If you only want to display 1 record at a time, you shouldn't be using a loop.

Initialise "A" (which I'll rename to "recordNumber" to be a lot more descriptive) to 0, and then show the content of that record:

Code:
var recordNumber = 0;
var numberOfRecords = 100;

function showCurrentNode() {
	document.getElementById('printid').value = PrintNode[recordNumber].childNodes[0].text;
}

function gotoPrevNode() {
	if (recordNumber > 0) {
		recordNumber--;
		showCurrentNode();
	}
}

function gotoNextNode() {
	if (recordNumber < numberOfRecords-1) {
		recordNumber++;
		showCurrentNode();
	}
}

So now you simply call the gotoNextNode and gotoPrevNode functions from your links / buttons.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
BillyRay,

Thanks, sorry I didn't get to reply sooner.

Yes, this did work. I resolved this part of the tool.
I can't tell you how thrilled I was.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top