Jump to content

[SOLVED] concatenation gone wrong


cleary1981

Recommended Posts

 

Hi, In the script below I am trying to achieve an output like BUS1 BUS2 BUS3 BUS4 but for some reason I keep getting BUS1 each time. If I take out the "BUS" I get 1, 2, 3, 4, 5 like I should. Whats up?

 

var bus = 1;
	var arr = document.getElementsByTagName("div");
		for (var i=0;i<arr.length;i++) {
			if (bus == arr[i].innerHTML) {
				bus = bus + 1;
			}				
		}
	bus = parseInt(bus);
	newObject.innerHTML = "BUS" + bus;

Link to comment
https://forums.phpfreaks.com/topic/134050-solved-concatenation-gone-wrong/
Share on other sites

According to your code the value of 'bus' will only get incremented if the innerHTML value of one of the divs in the loop exactly equals bus.

 

But, it looks like you may be populating those divs with something like 'BUS1', and 'BUS1' does not equal 1. Have you validated the values reurned in the array of divs?

 

 

That bit works fine. If I was to change the line

newObject.innerHTML = "BUS" + bus;

to

newObject.innerHTML = bus;

 

the script works. I get each value 1, 2, 3, 4, 5. But I want to add the word BUS to the output number. so I will have BUS1, BUS2, BUS3, etc. There is nothing wrong with the loop. Do I have to make the output number into a string or something?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.