Home > Mobile >  How do I make a conditional with hasClass that prints a text depending on the content of another div
How do I make a conditional with hasClass that prints a text depending on the content of another div

Time:06-15

For example if have:

<div>New York</div>

So I would like to print another div like this:

<div>Free Delivery</div>

How would I do that using JS?

CodePudding user response:

add id to element :

<div id="country">New York</div>

and then

let country = $('#country')

setInterval(()=>{
   if(country.text()=='New York'){
      // Your code
      // exemple 
      alert('Congratulations ! We Ship for Free for New York 
            Customers');
   }
},3000)

i hope it was useful

CodePudding user response:

you can do this with simple js condtion:

add a class/id to to the element <div id="country">New York</div>

and in your script:

const country = document.querySelector('#country');
const printdiv = '<div>Free Delivery</div>';

if(country && country.textContent.trim() == 'New York'){
  document.querySelector('#country').insertAdjacentHTML('afterend',printdiv)
}
  • Related