convert a string and number into a string

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

convert a string and number into a string

Post by gurjit »

hi all

i have a variable called theval i pass to a function. i then loop the variable and want to add the variable i to the end of the string and represent the entire thing into a string.

i have tried eval, concentate, tostring() but nothing works.

for example i do this

<input type=checkbox id="m3" onlcick="clickall('m3')" value=1 name="lid[]">


the function does this - the problem is on line:
if (allInputs.type == "checkbox" && allInputs.id.substring(0,10) == theval+i)


the variable theval is in a string but how do i make the variable 'i' part of the string in the if statement. i have tried to set it outside and all kinds of things but it doesent happen.

ANY HELP WOULD BE APPRECIATED. BEEN WORKING ON THIS ALL DAY....DOING MY HEAD IN RIGHT NOW.





function clickall(theval) {
var allInputs = document.getElementById("form1").getElementsByTagName("input")

for (var i = 1; i < allInputs.length; i++) {
if (allInputs.type == "checkbox" && allInputs.id.substring(0,10) == theval+i) {

alert("yes")

}

}
}
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

if I am interepreting your question correctly:
You have 'var theval' and you want to put a counter on the end of it.
So if theval='hello' you would wind up with 'hello0', 'hello1', etc.

To do that:

Code: Select all

var theval='test_';
for(i=start;i=end;i++)&#123;
  var newval=eval(theval)+i;
  alert(newval); // test_0, test_1, test_2, etc
&#125;
Skyzyx
Forum Commoner
Posts: 42
Joined: Fri Feb 14, 2003 1:53 am
Location: San Jose, CA

Post by Skyzyx »

Well, the way to convert a number to JavaScript is like so:

Code: Select all

var number=12345;
number=number.toString();
Post Reply