duukn
duukn

Reputation: 275

Need to change text of <a> with text from a variable list

I have a drop down that only shows up when an user access the website from a specific country. In this drop down there are two list elements with anchor tags in each. I need to change the anchor tags text with some data from my variable list.

Here is my list:

<div class="geo-location-button" style="text-decoration: none"><p>Take me to Global</p>
     <ul class="geo-list">
        <li class="one"><a id="first" href="#\"> E-Commerce Portal </a></li>
        <li class="two"><a id="second" href="#\"> Website </a></li>
      </ul>
    </div>

Here is what I have tried:

if (list[jo].url === undefined) {
   $('.geo-list .one a').text(list[jo].text2);
}

Here is an example of my variable list called 'jo'

'United States': {
      text: "Take me to United States",
      text2: "E-Commerce Portal",
      text3: "Website",
      url: '',
      url2: 'https://www.bestbuy.com',
      url3: 'http://www.google.com'
      },  


'United Kingdom': {
      text: "Take me to  UK",
      url: 'http://www.google.co.uk'
      }, 

As you can see from my script, I want to change the text of the anchor within the list only when a country from my variable list does not have a value for 'url', therfore having more than one options in both text values (text2,text3).

Upvotes: 0

Views: 63

Answers (3)

Thomas R. Koll
Thomas R. Koll

Reputation: 3139

Just a typo there, you have one instead of .one.

$('.geo-list .one a').text(list[jo].text2);

Another thing if your url is '' then url === undefined will return false. What you want is url === undefined || url == ''

Upvotes: 2

Kunal Bindal
Kunal Bindal

Reputation: 11

Please use . before a class while refering in jquery

if (list[jo].url === undefined) {
   $('.geo-list .one a').text(list[jo].text2);
}

Upvotes: 1

ellimist
ellimist

Reputation: 616

You're missing the dot in front of the .one class selector.

if (list[jo].url === undefined) {
  $('.geo-list .one a').text(list[jo].text2);
  $('.geo-list .two a').text(list[jo].text3);
  ... etc
}

Upvotes: 1

Related Questions