Forum Moderators: open

Message Too Old, No Replies

get element by class

         

Kevin McLean

6:57 pm on Jan 24, 2009 (gmt 0)

10+ Year Member



Hi everybody. I'm a newbie in web designing so that faced a lot of problems concerning combination of css and js in developing my web page.
First of all, in my css file I use classes and it does not allow me to embody my idea: I want a block with info to appear after clicking on a nick name.
Javascript file which is in charge of block behaviour.

<script language="JavaScript" type="text/javascript">
function getElementsByClass(searchClass,node,tag) {
var classElements = new Array();
if ( node == null )
node = document;
if ( tag == null )
tag = '*';
var els = node.getElementsByTagName(tag);
var elsLen = els.length;

var pattern = new RegExp("(^¦\\s)"+searchClass+"(\\s¦$)");
for (i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
classElements[j] = els[i];
j++;
}
}
return classElements;
}

function show_bar(ev,nom) {
MouseX = ev.clientX + document.body.scrollLeft;
MouseY = ev.clientY + document.body.scrollTop;
obj = document.getElementById(nom);
obj.style.top = MouseY + 10-document.getElementByClass("wrap").offsetParent.offsetTop-40+"px";
obj.style.left = MouseX-document.getElementByClass("wrap").offsetParent.offsetLeft+"px";
obj.style.visibility = "visible";

}function hide_bar(nom) {document.getElementByClass(nom).style.visibility="hidden"}</script>

webpage:
<snip>
so when I click on a nickname in one of 3 tables nothing happens.

The second problem, there is a gigant space after layout caused by this js, is it possible to avoid it?
Could somebody help me with that?
Thank you

[edited by: eelixduppy at 8:19 am (utc) on Jan. 25, 2009]
[edit reason] no personal URLs, please [/edit]

daveVk

3:46 am on Jan 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function show_bar(ev,nom) {
MouseX = ev.clientX + document.body.scrollLeft;
MouseY = ev.clientY + document.body.scrollTop;
obj = document.getElementById(nom);
obj.style.top = MouseY + 10-document.getElementByClass("wrap").offsetParent.offsetTop-40+"px";
obj.style.left = MouseX-document.getElementByClass("wrap").offsetParent.offsetLeft+"px";
obj.style.visibility = "visible";

}function hide_bar(nom) {document.getElementByClass(nom).style.visibility="hidden"}

I assume they both should be the same, that is ById or ByClass.

Also the function you show is getElementsByClass, perhaps all calls to getElementByClass should be changed to getElementById or at least getElementsByClass()[0]

Kevin McLean

5:16 pm on Jan 25, 2009 (gmt 0)

10+ Year Member



I changed all to getElementsByClass[0], but still nothing happens :(

tbarbedo

2:43 pm on Jan 26, 2009 (gmt 0)

10+ Year Member



You can always use jQuery with selectors to get elements with a certain class...

$(".classname").each( function() {

// do something with each element with the class

)};