Tuesday, 15 January 2013

javascript - How to create a function to center windows -



javascript - How to create a function to center windows -

i'm using jqgrid, , focus popup add, delete , edit, need utilize parameter beforeshowform before show window, shows center of screen. problem have same code these 3 functions.

the function follows:

{ // edit alternative beforeshowform: function(form) { var dlgdiv = $("#editmod" + $(this)[0].id); var parentdiv = dlgdiv.parent(); var dlgwidth = dlgdiv.width(); var parentwidth = parentdiv.width(); var dlgheight = dlgdiv.height(); var parentheight = parentdiv.height(); var parenttop = parentdiv.offset().top; var parentleft = parentdiv.offset().left; dlgdiv[0].style.top = math.round(parenttop / 2) + "px"; dlgdiv[0].style.left = math.round(parentleft + (parentwidth-dlgwidth )/2 ) + "px"; } },

in order reuse same code, create separate function writing same amount of code. tried create next function:

function:

function test(dlgdiv) { var parentdiv = dlgdiv.parent(); var dlgwidth = dlgdiv.width(); var parentwidth = parentdiv.width(); var dlgheight = dlgdiv.height(); var parentheight = parentdiv.height(); var parenttop = parentdiv.offset().top; var parentleft = parentdiv.offset().left; dlgdiv[0].style.top = math.round(parenttop / 2) + "px"; dlgdiv[0].style.left = math.round(parentleft + (parentwidth-dlgwidth )/2 ) + "px"; }

in grid:

{ // edit alternative beforeshowform: function(form) { var dlgdiv = $("#editmod" + $(this)[0].id); test(dlgdiv); } },

but continued without giving. says value dlgdiv not defined. know how solve this?

the issue lies in selector utilize here:

var dlgdiv = $("#editmod" + $(this)[0].id);

whatever element you're trying isn't beingness returned, because selector can't find it. should revise selector reflect id of element you're attempting find.

if you've done , still have issue, problem lies context , this doesn't mean think means in above line of code. you'll have show more code before can elaborate on that, though.

javascript jqgrid

No comments:

Post a Comment