0

I am trying to get values from a data set. My function has to loop from 1 to 7 to get my opening hours for 7 days. what i need to do is:

Where openHours[i] the "i" stands for a number between 1 and 7. I would like to attach that number on the word "openHours" in order to create a new word ex. "openHours1"

What would be the best way to do that? The "alert(i);" is working fine and is generating numbers from 1 to 7. I just need to attach those numbers on the string.

So far i tried everything that can comes in my head but I don't use javascript often so i am kind of stuck.

Thanks for the help.

function hoursFunction()
{  
var i =0;
alert("Hello");
for(i=1;i<8;i++)
{
    alert(i);
    alert(hoursForm.openHours[i].value);
}

}
3
  • Do you mean string concatenation? Javascript is dynamic typed, so doing something like alert('hello' + 1) will give you 'hello1'. Commented Apr 6, 2012 at 20:49
  • What is the value of hoursForm.openHours? Commented Apr 6, 2012 at 20:49
  • @FlorianMargaine yes that's what I meant. Sorry for the wording. The value of openHours is a number the thing is the correct variable would be "openHours1" where the number 1 would get attached to the string somehow. Commented Apr 6, 2012 at 20:55

2 Answers 2

1

EDIT: After your comment, it would appear you are trying to get the following variable:

alert(document.getElementById("openHours" + i).value);

However, I would strongly consider using jQuery (or another library) to handle this, and you could do the following:

$("#openHours" + i).val();
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the bracket notation to access properties - they're the same thing as indices in JavaScript.

alert(hoursForm[openHours + i].value);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.