Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using elements that are added to an array with (document.getElementById('ID'))

Tags:

javascript

Why does this code not work?

var all_obj_element= new Array();
all_obj_element[0]= document.getElementById('Img3');            
alert(all_obj_element[0].style.width);

The alert shows an empty box!

like image 256
hamze Avatar asked Feb 25 '23 02:02

hamze


1 Answers

Because you haven't set the width. Here is how you get the computed style value of an element:

var computedStyle = function (el,style) {
    var cs;
    if (typeof el.currentStyle != 'undefined'){
        cs = el.currentStyle;
    }
    else {
        cs = document.defaultView.getComputedStyle(el,null);
    }
    return  cs[style];
}

Now let's get the value:

var element = document.getElementById('Img3');

alert(computedStyle(element,'width'));
like image 141
Ibu Avatar answered Feb 26 '23 14:02

Ibu