1
<div id="image_div">
<input type="checkbox" />
<img src="1.jpg" />
<input type="checkbox" />
<img src="2.jpg" />
<input type="checkbox" />
<img src="3.jpg" />
<input type="checkbox" />
</div>

On page load, I return a JSON array:

{
    "src":["lml7x8nJzI.jpg","TaR7dALIPJ.jpg","TDE2pWgIfa.jpg","tUtuDx1BEf.jpg"],
    "checked":["lml7x8nJzI.jpg","TaR7dALIPJ.jpg","tUtuDx1BEf.jpg"]
}
  • The src contains all the source names of image and checked is the checked type of check boxes.
  • The checked defines which image is checked (every image has a check box).

I need the final HTML like this:

<div id="image_div">
<input type="checkbox" />
<img src="lml7x8nJzI.jpg" />   
<input type="checkbox" checked />              //this is a checked one
<img src="TaR7dALIPJ.jpg" />    
<input type="checkbox" checked/>                 //this is a checked one
<img src="TDE2pWgIfa.jpg" />
<input type="checkbox" />                    //this is not
<img src="tUtuDx1BEf.jpg" />     
<input type="checkbox" checked />  //this is a checked one
</div>

I can use array contains in JavaScript, or is there a better way?

My attempt:

var initial = "<ul class='gallary'>";
var middle = "";
var end = "</ul>";
var i;

for (i = 0; i < data.src.length; i++)
{
    if (data.checked.contains(data.src[i]))   // How can I do this?
    {
        middle = middle +
            "<li><img src=" +
            "http://localhost/project/user/" +
            "<?php echo $this->session->userdata('username');?>" +
            "/pages/" +
            "<?php echo $this->uri->segment(3);?>" +
            "/images/gallery/" +
            data.src[i] +
            " />" +
            "<br /><input type='checkbox' value=" +
            data.src[i] +
            " checked/></li>";
    }
    else
    {
        middle = middle +
            "<li><img src=" +
            "http://localhost/project/user/" +
            "<?php echo $this->session->userdata('username');?>" +
            "/pages/" +
            "<?php echo $this->uri->segment(3);?>" +
            "/images/gallery/" +
            data.src[i] +
            " />" +
            "<br /><input type='checkbox' value=" +
            data.src[i] +
            " /></li>";
    }
}

var complete = initial + middle + end;
$("#project_gallary_layout").html(complete);

3 Answers 3

3

You can use indexOf method of Array

var a = ['a', 'b', 'c'];
if (a.indexOf('a') > -1 ) {
    //element is in array
} else {
    //element is not in array
}

Note that indexOf method is absent in IE6, but there is an implementation available by Mozilla on the same page.

Sign up to request clarification or add additional context in comments.

Comments

1

Here's my complete solution using indexOf

$(function(){
  var url = 'http://localhost/project/user/<?php echo $this->session->userdata('username'); ?>/pages/<?php echo $this->uri->segment(3); ?>/images/gallery/'
    , complete = '<ul class="gallery">'

  data.src.forEach(function(src){
    complete += '<li><img src="' + url + src + '"><input type="checkbox" value="' + src + '"'
    if(data.checked.indexOf(src) > -1){
      complete += ' checked'
    }
    complete += '></li>'
  })

  $("#project_gallary_layout").html(complete);
})

Comments

1

A better approach would be to replace

{"src":["lml7x8nJzI.jpg","TaR7dALIPJ.jpg","TDE2pWgIfa.jpg","tUtuDx1BEf.jpg"],
 "checked":["lml7x8nJzI.jpg","TaR7dALIPJ.jpg","tUtuDx1BEf.jpg"]}

with

{"src":[
    {"src": "lml7x8nJzI.jpg", "checked": true},
    {"src": "TaR7dALIPJ.jpg", "checked": true},
    {"src": "TDE2pWgIfa.jpg", "checked": false},
    {"src": "tUtuDx1BEf.jpg", "checked": true}]
}

for instance.

Then you could just write

for(i=0;i<data.src.length;i++) {
    var elem = data.src[i];
    var checked = "";
    if (elem.checked)
        checked = "checked";
    middle = middle + 
        "<li><img src=" +
        "http://localhost/project/user/" +
        "<?php echo $this->session->userdata('username');?>" +
        "/pages/" +
        "<?php echo $this->uri->segment(3);?>" +
        "/images/gallery/" +
        elem.src +
        " />" +
        "<br /><input type='checkbox' value=" +
        elem.src +
        " " + 
        checked +
        "/></li>";
    }
}

The only thing different between the two strings you make is the attribute checked (which should really be checked="checked" if you want to follow standards), so I'd prefer setting a variable checked to either an empty string or "checked" depending on whether the checkbox is checked or not.

2 Comments

Nice one,this one i was thinking first...but i am little bit new here,how can i manipulate an array like this ??
@DileepDil: Do you mean the array in the json object? If the returned object is data as in the example you just use data.src[0].src, data.src[0].checked, data.src[1].src, data.src[1].checked etc. for accessing the values. You can assign to them like you would any other variable.

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.