views:

58

answers:

1

I use this code to determine checkbox width and height:

var chk = $('input[type="checkbox"]:first');

chkWidth = chk.innerWidth() 
   + parseInt(chk.css('margin-left').replace('px', '')) 
   + parseInt(chk.css('margin-right').replace('px', '')); /*IS there better way instead of px replace?? */

chkHeight = chk.innerHeight()
   + parseInt(chk.css('margin-top').replace('px', '')) 
   + parseInt(chk.css('margin-bottom').replace('px', ''));

fieldWidth = gamefield.innerWidth();
fieldHeight = gamefield.innerHeight();

Then i try to fill div with the number of checkboxes that exactly fits given div. In fact i get a lot of checkboxes out of div.

Is there anyway to solve this? Thank you!

+1  A: 

leaving other things as such, this makes the difference (on FF3.5):

    var cols=Math.floor(fieldWidth/chkWidth);
var rows=Math.floor(fieldHeight/chkHeight);
var html='';
for(var i=0;i<=rows;i++) //if yo use i<rows there is space for one more row
{
    for(var j=0;j<cols;j++)
    {
        html +="<input type='checkbox'/>";
    }
}

gamefield.html(html);
TheVillageIdiot
working in Chrome also.
TheVillageIdiot
You are right. I used ceil instead of floor.Your version is displayyed correctly. But if i use append instead of html. I get such picture in FF and Chrome http://img2.pict.com/4b/14/3a/3032352/0/clipboard2.jpg
Overdose
Hmm, I noticed it happens because of the extra space (why is it added ??...).
Overdose