0

Can i use native javascript for loop within jquery code? I have a construct in which $.each does not seem feasible. How can I loop in this situation?

the code inside the for loop is not working. any advice would be appreciated.

if (item.relate){
    $(prodHeading).append(item.name);
    if (item.image) {$(prodContent).append(item.image);}
    if (item.description) {
        $(prodContentText).append(item.description); 
        $(prodContent).append(prodContentText);
    }
    $(prodBox).append(prodHeading);
    $(prodBox).append(prodContent);

    //going to look for other attached items

    for (j=0; j<10; j++){
        var prodBox = $(document.createElement('div')).attr('class','productBox');
        var prodHeading = $(document.createElement('div')).attr('class','productBoxHeading');
        var prodContent = $(document.createElement('div')).attr('class','productBoxContent');
        var prodContentText = $(document.createElement('div')).attr('class','text');

        if ('item.image'+j) {$(prodContent).append(item.image);}
        if ('item.description'+j) {
            $(prodContentText).append(item.description); 
            $(prodContent).append(prodContentText);
        }
        $(prodBox).append(prodHeading); 
        $(prodBox).append(prodContent);
    }
}
3
  • What do you want to loop through? Commented Aug 18, 2011 at 11:37
  • 1
    Don't you think your a missing a var in j=0? like for(var j = 0; j<10; j++= Commented Aug 18, 2011 at 11:39
  • Are you getting any errors? The loop itself is standard... Commented Aug 18, 2011 at 11:39

3 Answers 3

1

First,

for (j=0; j<10; j++){

should be using a declared variable:

for (var j=0; j<10; j++){

Second,

if ('item.image'+j) {

does not make much sense. Every such string will pass an if condition. If you are looking whether an element exists, use:

if ($('item.image'+j).length) {
Sign up to request clarification or add additional context in comments.

2 Comments

the ('item.image'+j) is actually calling a json element. and it is not working like that. this code is inside a $.each loop. can you suggest what to do if i want to toggle through all the item.image(0-9)??
@amit: You're currently combining strings, nothing more. If you want accessing a dynamic property of an object, you can use item['image' + j], which equals to item.image0 if j is equal to 0, etc.
0

Yes, you can use regular javascript with JQuery.

What is this if statement meant to do?

if ('item.description'+j)

There is no comparison happening, so it will always be true.

Comments

0
  for (var j=0; j<10; j++){
                    var prodBox = $(document.createElement('div')).attr('class','productBox');
                    var prodHeading = $("<div/>",{class:'productBoxHeading'});    
                    var prodContent = $("<div/>",{class:'productBoxContent'}); 
                    var prodContentText = $("<div/>",{class:'text'}); 


                    if (item.image+j) {prodContent.append(item.image);}
                    if (item.description+j) {
                        prodContentText.append(item.description); 
                        prodContent.append(prodContentText);
                        }
                    prodBox.append(prodHeading); 
                    prodBox.append(prodContent);
                    }

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.