Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

javascript unable to get previousSibling Class

I am trying to change the div class so i can add the functionality to it

Here is my JS Code

idName is giving me name as Tuesday

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

console.log(document.querySelector("#w1_"+idName.split('_')[1]).previousSibling);
            document.querySelector("#w1_"+idName.split('_')[1]).previousSibling.classList.remove('specialCheckbox2');
            document.querySelector("#w1_"+idName.split('_')[1]).previousSibling.classList.add('specialCheckbox3');
            document.querySelector("#w1_"+idName.split('_')[1]).disabled = true;

and here my html

<div class="specialCheckbox2"><input class="checktip" type="checkbox" value="1" name="w1_Tuesday" id="w1_Tuesday"><label for="w1_Tuesday"></label></div>

>Solution :

The element with the w1_Tuesday id is the first sibling. There isn’t one before it, so previousSibling will return null.

It looks to me like you want to get the surrounding div with the specialCheckbox2 class. That can be gotten with parentElement like this:

const parent = document.querySelector("#w1_"+idName.split('_')[1]).parentElement;
parent.classList.remove('specialCheckbox2');
parent.classList.add('specialCheckbox3');

Or parentNode is also an option:

const parent = document.querySelector("#w1_"+idName.split('_')[1]).parentNode;
parent.classList.remove('specialCheckbox2');
parent.classList.add('specialCheckbox3');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading