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
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?

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.