?
Solved

getElementByClass in a HTML

Posted on 2010-11-10
19
Medium Priority
?
1,728 Views
Last Modified: 2012-05-10
I have a HTML webpage with the 3 classes.-sidebar1 is one class

Ia m trying to do this:
     var Height = document.getElementByClass('sidebar1').offsetHeight;

This is not working.
Can you please let me know how I can get the offset of a Class?

similarly I am trying to use the following function.
document.getElementByClass('sidebar1').style.height  which is also not working
0
Comment
Question by:vkchaitu82
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 3
  • +2
19 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34108451
getElementsByClassName( name )

This gives you an array that you can work through
0
 

Author Comment

by:vkchaitu82
ID: 34108467
Thankq.
can you please elaborate on this? i am not clear!!!
0
 
LVL 57

Expert Comment

by:HainKurt
ID: 34108481
try

var Height = document.getElementByClass('sidebar1')[0].offsetHeight;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:vkchaitu82
ID: 34108501
thankq.
is it  document.getElementByClass (or)  document.getElementsByClass??
also I have so many classes (3). so what is this [0] stand for?
0
 
LVL 57

Expert Comment

by:HainKurt
ID: 34108514
I found this function
<div id=d1 class=a width=100>a</div>
<div id=d2 class=b>b</div>
<div id=d3 class=b>b</div>
<div id=d4 class=a width=200>a</div>

<script>
function getElementsByClassName(classname, node)  {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
    if(re.test(els[i].className))a.push(els[i]);
    return a;
}

var h1 = getElementsByClassName("a",document)[0].offsetHeight;
var h2 = getElementsByClassName("a",document)[1].offsetHeight;
alert(h1 + " - " + h2);
</script>

Open in new window

0
 
LVL 57

Accepted Solution

by:
HainKurt earned 1500 total points
ID: 34108542
use provided function above and use

var h1 = getElementsByClassName("a",document.body)[0].offsetHeight;
var h2 = getElementsByClassName("a",document.body)[1].offsetHeight;

or better if you know where are these elements, provide the container node as second parameter

var myContainer = document.getElementById("myContainerID");
var h1 = getElementsByClassName("a", myContainer)[0].offsetHeight;
var h2 = getElementsByClassName("a", myContainer)[1].offsetHeight;
0
 

Author Comment

by:vkchaitu82
ID: 34108562
Thanks Hainkut.
I am still not clear with the array/a/Mycontainter?

I think a=sidebar1 (my class name)
what are the other 2?
0
 
LVL 7

Expert Comment

by:wdfdo1986
ID: 34108716
try with this
var arr = getElementsByClassName('sidebar1');
for (var i = 0 ; i < arr.length ; i ++ ){
  var Height = document.getElementsByClassName('sidebar1').height;
 //do what u want to do with height
}
0
 
LVL 57

Expert Comment

by:HainKurt
ID: 34108737
just use the function above with these lines

var h1 = getElementsByClassName("a",document.body)[0].offsetHeight;
var h2 = getElementsByClassName("a",document.body)[1].offsetHeight;

if your elements are say in a div with id=myContainerID

<div id=myContainerID>
...
</div>

then you can use second sample to speed up element searching... here is both usage together
<div id=myContainerID>
<div id=d1 class=a>a</div>
<div id=d2 class=b>b</div>
<div id=d3 class=b>b</div>
<div id=d4 class=a>a</div>
</div>

<script>
function getElementsByClassName(classname, node)  {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
    if(re.test(els[i].className))a.push(els[i]);
    return a;
}

var h1 = getElementsByClassName("a",document.body)[0].offsetHeight;
var h2 = getElementsByClassName("a",document.body)[1].offsetHeight;
alert(h1 + " - " + h2);

var myContainer = document.getElementById("myContainerID");
var h1 = getElementsByClassName("a",myContainer)[0].offsetHeight;
var h2 = getElementsByClassName("a",myContainer)[1].offsetHeight;
alert(h1 + " - " + h2);
</script>

Open in new window

0
 

Author Comment

by:vkchaitu82
ID: 34109413
My element is in div = class.

 var Height = document.getElementsByClassName('sidebar1')[0].offsetHeight;
    document.getElementsByClassName('content')[0].style.height = Height + "px" ;

Both worked for me .I did noting else,
The Question I have is :
what does array[0] stand here?..I am really confused.
0
 
LVL 7

Expert Comment

by:wdfdo1986
ID: 34109967
ok
When u say getElementsByClassName it will return an array of objects whose class name is that name u given.
I hope u know that there can be several tags with same classname.
So it will return the array that contains the classname u given.
If u use it only for once then that array will only contain one element.
If u use more there will be more elements in the array.
0
 
LVL 5

Assisted Solution

by:SimonDard
SimonDard earned 500 total points
ID: 34110224
I recommend checking out jQuery for this.

If you have not heard of jQuery before: at it's core it is a relatively small library that gives you easy access to elements by id, class, type etc. making code easier to read and understand.

Your first question in jQuery would be: $(".sidebar1").height();
Changing it is as easy as:  $(".content").css("height",  ($(".sidebar1").height())+"px");

As you can see: no fiddling around with arrays here.
0
 
LVL 57

Assisted Solution

by:HainKurt
HainKurt earned 1500 total points
ID: 34112008
but

document.getElementsByClassName('...)

does not exists, no such functiuon... what browsers are you using?
Thats why I posted the custom function... it returns you an array (a 0 based index of objects)
when it finds one lement, you get it by adding [0] to return value...

var divs =  getElementsByClassName("myDataDiv", document.body);
if (divs.length>0) {
 var div1=divs[0];
...
}
0
 

Author Comment

by:vkchaitu82
ID: 34112095
wdfdo1986- Thanks f the reply.
I am very new to javascript. can you tell me the what are the serveraltags with the given class name?

Is there any restrictions on the Browser to use this API?
As it is java script I think that will not be the case. but want to confirm.
0
 

Author Comment

by:vkchaitu82
ID: 34112120
HainKurt:
I am using crome.
Initially I also taught it is not there. but it worked in Crome/Firefox.

In IE it is not working?
0
 

Author Comment

by:vkchaitu82
ID: 34112151
Looks I am back to the problem.
so here are my questions again.

1) I am very new to javascript. can you tell me the what are the serveraltags with the given class name?

2) the script does not work in IE. should I look into jquery?
0
 
LVL 5

Expert Comment

by:SimonDard
ID: 34113074
1) getElementsByClassName gives you all the elements ('tags') using that class. Remember that several elements can share the same class.

2) jQuery is cross browser compatible, so looking into jQuery will help. Also, since you're new to javascript, using jQuery will make your work easier, although it doesn't hurt to know the underlying principles.
0
 
LVL 57

Assisted Solution

by:HainKurt
HainKurt earned 1500 total points
ID: 34115380
the function I provided before, getElementsByClassName, should work fine in all browsers... why do you say it is not working? I tested it in IE

function getElementsByClassName(classname, node)  {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
    if(re.test(els[i].className))a.push(els[i]);
    return a;
}
0
 
LVL 7

Expert Comment

by:wdfdo1986
ID: 34119506
I think I gave u a solution which assist u.
Although selection is urs I am worried about not rating my answer under assisted solution.
0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question