cleary1981 Posted November 24, 2008 Share Posted November 24, 2008 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; Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 24, 2008 Share Posted November 24, 2008 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? Quote Link to comment Share on other sites More sharing options...
cleary1981 Posted November 24, 2008 Author Share Posted November 24, 2008 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? Quote Link to comment Share on other sites More sharing options...
cleary1981 Posted November 24, 2008 Author Share Posted November 24, 2008 oh I see what you are saying now. I used split result to solve this Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.