1

I have a div structure like following:

<div id="image0" style="margin-bottom:10px; "></div>
<div id="image1" style="margin-bottom:10px; "></div>
<div id="image2" style="margin-bottom:10px; "></div>
<div id="image3" style="margin-bottom:10px; "></div>
<div id="image4" style="margin-bottom:10px; "></div>

My jQuery code is like follows:

var res = question.text.split(imgQues);
    if (question.image != '') {
        for (var i = 0; i < res[0]; i++) {
            jQuery('#image' + "" + i).html(question.image);
        }
    } else {
        jQuery('#image' + "" + i).html('');
    }

In my case if the res[0] is "5" code will loop to display 5 image and if res[0] value is "1" then its 1 image.

With my above code, it displays 5 images at the beginning but for next time it is still displaying 5 image not 1.

4
  • Try clear #image## every time. Commented Apr 5, 2016 at 8:14
  • 1
    possibly your else will have undefined i variable Commented Apr 5, 2016 at 8:16
  • It is an undefined Question, think how you can excess a loop variable outside the scope ... watch out for errors in console Commented Apr 5, 2016 at 8:20
  • Yes. i forgot to remove i. Commented Apr 5, 2016 at 8:20

3 Answers 3

1

The way you are doing it, you are replacing the content of the divs each time. So when res[0] is smaller than the total number of divs, the divs with larger IDs would be left untouched.

To solve this problem, you first have to define the total number of these image divs, say in your example, there are at most 5 of them.

so: var max = 5;

Now change your code a bit so we clear those larger IDs :

var max = 5;
var res = question.text.split(imgQues);
var i;
// Clear them out first
for (i = 0; i < max; i++){
    jQuery('#image' + "" + i).html('');
}
if (question.image != '') {
    for (i = 0; i < res[0]; i++) {
        jQuery('#image' + "" + i).html(question.image);
    }
}

Alternatively, it might be more desirable to do it this way, so there isn't a need to keep track of how many image divs there are:

HTML:

<div id="image-container"></div>

CSS:

.my-image {
    margin-bottom:10px;
}

JavaScript:

var imgContainer = jQuery("#image-container");
imgContainer.html(""); // Clear contents
if (question.image != '') {
    var res = question.text.split(imgQues);
    for(var i = 0; i < res[0]; i++){
        var newImage = jQuery("<div class=\"my-image\"></div>");
        newImage.html(question.image);
        imgContainer.append(newImage);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for idea
1
    for (var j = 0; j < res; j++) {
        for (var i = 0; i < res[j]; i++) {
            $('#image' + "" + i).html(question.image);
        }
    }

You have to add two loops like this.

Comments

0

Oh I forgot to clear div. Its working now. Thank you.

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.