Home > database >  Not Able to Select a Class With Parent Specification in Vanilla JavaScript
Not Able to Select a Class With Parent Specification in Vanilla JavaScript

Time:07-24

Can you please let me know how can I get this done in vanilla JavaScript? I am able to select the .pay-plan-price text which it's parent has active class like this in jQuery

 var fee = $('.plan-box.active .pay-plan-price').text();

But I need to do same select in core js format. I tried this

var fee = document.getElementsByClassName('.plan-box.active .pay-plan-price');
console.log(fee)

but in return I am getting HTNLCollection instead of the text of .pay-plan-price which has parent .plan-box.active

enter image description here

CodePudding user response:

You can use document.querySelector to select an element using a similar selector as jquery accepts:

document.querySelector('.plan-box.active .pay-plan-price');

And then of course you need to get a text from that element, so use also textContent or innerText property on the returned element.

Example:

var fee = document.querySelector('.plan-box.active .pay-plan-price').textContent;
console.log(fee);
  • Related