1

I have an element in CSS. I'm using:

ul.bjqs-controls.v-centered {to add style to it}

But in JS I need to define it and I don't have and Id, the only way to define is ul.bjqs-controls.v-centered.

I'm new in JS.

Should I use getElementById? Or what?

EDIT:

I'm trying to use jquery, but it's first time I use it, I'm trying to change element's (ul.bjqs-controls.v-centered) width to window width on page resize, but it doesn't seem to work :/

$(window).resize(function() {
 $("ul.bjqs-controls.v-centered").width()=$(window).width();
});
user1804119
  • 142
  • 3
  • 12
  • 1
    there is a function getElementsByClassName or if you use jquery.... just type $("ul.bjqs-controls.v-centered") – Paul Moldovan Jun 29 '13 at 10:46
  • *"But in JS I need to define it"* I don't even know what you mean that. Do you want to get a reference to the element? Do you want to create an element? "Defining an element" is not a phrase that is commonly used in this context. – Felix Kling Jun 29 '13 at 11:08
  • From all the answers I have a conclusion, that jquery is easier way, I think i'll use it, because I'm green in JS and jquery and it's hard to me to understand what nodelist is and it's also uncompatible with some IE versions. – user1804119 Jun 29 '13 at 11:12
  • Felix Kling, I was unsure of using word 'define' here. English is not my main language. I meant, that I need to attach element to a variable and I don't know what function do I use to get my element which does'nt have an ID. Hope you understand now. – user1804119 Jun 29 '13 at 11:16

3 Answers3

1

getElementsByClassName("classname") will return a nodeList. You can loop in it and reach item what you want.

dorukce
  • 77
  • 10
  • But getElementsByClassName does not work on IE 5,6,7 and 8 right? – Edper Jun 29 '13 at 10:50
  • @Edper, yeah you are right, there is a solution is here: http://stackoverflow.com/questions/7410949/javascript-document-getelementsbyclassname-compatibility-with-ie – dorukce Jun 29 '13 at 10:53
  • @Quentin - what's the difference between an array and a nodelist? Isn't a nodelist an array? – Saturnix Jun 29 '13 at 11:35
  • 1
    @Saturnix — There are a lot of similarities, but the obvious difference is that it will fail if you try to `instanceof foo === Array` – Quentin Jun 29 '13 at 11:38
  • 1
    @Saturnix: No it isn't. E.g. you cannot add elements to a NodeList and it does not have any of the array methods, i.e. `.push`, `.pop`, `.map`, `.slice`, `.forEach`, etc. – Felix Kling Jun 29 '13 at 14:48
1

Please have a look at http://api.jquery.com/category/traversing/tree-traversal/

You can find the element by using other identified elements or using classes as people mentioned above.

themazz
  • 1,107
  • 3
  • 10
  • 29
0

you can use jquery like below

$(".ul.bjqs-controls.v-centered").html() 

like that

Manish Parmar
  • 859
  • 1
  • 11
  • 19
  • What if the OP cannot or doesn't want to use jQuery? From the `javascript` tag description: *"Unless a tag for a framework/library is also included, a pure JavaScript answer is expected."* – Felix Kling Jun 29 '13 at 11:10