Log in

View Full Version : Jquery $('.class') select only one



keyboard
09-02-2012, 12:36 AM
Hey everyone!

I have several divs which all have the class content. Only one div is made visible at a time using this code -



function hidedivs() {
//HIDE UN-NEEDED DIVS
var contentid=new Array();
contentid[0]="con1";
contentid[1]="con2";
contentid[2]="con3";
for(i=0;i<contentid.length;i++) {
document.getElementById(contentid[i]).style.display = 'none';
}
}
function swapcontent(contentid) {
hidedivs();
//document.getElementById(contentid).style.display = 'block';
$('#' + contentid).fadeOut().delay(50).fadeIn(800);
$('#sideBar').fadeOut().delay(50).fadeIn(800);
}

What I'd like to do is get the height of the div that is currently visible and assign it to a variable like this, but it doesn't work...


$(".content").each(function (w) {
if(w.style.display != "none") {
var heightvar = w.height();
} else {
}
});

Any help?

jscheuer1
09-02-2012, 12:45 AM
There could also be other problems. However, according to the manual:

http://api.jquery.com/jQuery.each/

The first parameter in the each function is the index. so w will never have a style property.

You can either do:


$(".content").each(function (i, w) {
if(w.style.display != "none") {
var heightvar = w.height();
} else {
}
});

Or:


$(".content").each(function () {
if(this.style.display != "none") {
var heightvar = w.height();
} else {
}
});

If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.