0

Trying to get the previous class of an elements using JavaScript.

<li class="dynamic_class second_dynamic_class xx "></li>
<li class="dynamic_class second_dynamic_class"></li>

I want to put into a var and compare the dymanic_class of the <li> with the class="xx".

ONLY pure JavaScript allowed !

Further Details:

<li class='day_1 24.02.2013' data-name='day_1 24.02.2013' >
more code
</li>
<li class='day_2 25.02.2013 today' data-name='day_2 25.02.2013' >
more code
</li>
<li class='day_3 26.02.2013' data-name='day_3 26.02.2013' >
more code
</li>
<li class='day_4 27.02.2013' data-name='day_4 27.02.2013' ></li>

First I need to select the < li> with the class today than read the "previous" < li> class of the same element, i need to obtain the class="day_1" into a var.

5
  • 1
    Could you please further explain what you are trying to achieve? Commented Feb 25, 2013 at 10:57
  • What is a "previous class"? Your question is not clear. Commented Feb 25, 2013 at 11:07
  • I have to select the specific < li > element with the class="xx" and read this element other classes, finaly compare one of these classes with anothe element class. Commented Feb 25, 2013 at 11:08
  • What if more than one <li> element has the class "xx"? Commented Feb 25, 2013 at 11:09
  • Impossible, I give the xx class using another javascript Commented Feb 25, 2013 at 11:12

2 Answers 2

1

Try this:

var results = document.getElementById('targetId').className.split(' ');
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately I have no targetId to identify the element. Starting from your eg. I tryed: var element = document.getElementsByTagName("li"); if (element.className == "today") { //need some code } but I'm lost, need some extra code!
var elements = document.getElementsByTagName("li"); getElementsByTagName returns array of elements, so instead of elements.className you should write elements[0].className (or iterate through for and get all element's classNames)
0

Finally I've made it, the code looks like this:

I have multiple elements with different classes:

<li class='day_1 24.02.2013' data-name='day_1 24.02.2013' >more code</ li>
<li class='day_2 25.02.2013 ' data-name='day_2 25.02.2013' >more code</ li>
<li class='day_3 26.02.2013 today' data-name='day_3 26.02.2013' >more code</li>
<li class='day_4 27.02.2013' data-name='day_4 27.02.2013' >more code</ li>

I wanted to put into a variable (using just javascript code) the first class of the <li> element which also has the today class so I used the following code:

var now=' ';
var demo= document.getElementsByTagName("li");
for (var i = 0; i < demo.length; i++ ) {

if (demo[i].className.indexOf('today')!=-1) {
    if (demo[i].className.indexOf('day_1')!=-1){
        now = 'sunday';
    }else if(demo[i].className.indexOf('day_2')!=-1) {
        now = 'monday';
    }else if(demo[i].className.indexOf('day_3')!=-1) {
        now = 'tuesday';
    }else {
        now = 'wednesday';
    }   
} /* first if - ends here */

} /* for - ends here */

 alert(now);

Now you can select the first or second class of the <li> with the class "today".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.