Nuru Salihu
Nuru Salihu

Reputation: 4948

String exist in javascript , JQuery

Please i have the following code

if(page.language() === 'FR' && /myid/.test(active.item)){

}

I confirm my page.language return FR . my active.item returns object below

li#myid.item.fr-hide, prevObject: _.fn.init[1], context: a.link.pjaxload]

I am trying to check if my condition above is true. However its always false despite the fact that the string exist in the object.

I tried

"myid".test(active.item)

and encountered not a function error.

I tried

active.item.indexOf("myid")

and encounter an error as well. Please how do i solve this ? Any help would be appreciated.

Upvotes: 1

Views: 49

Answers (2)

Nuru Salihu
Nuru Salihu

Reputation: 4948

Like magus said above, active.item is a jQuery object. Below is what works for me

var $elementid = active.item.get(0).id;

if(active.item) {
   if(page.language() === 'FR' && /myid/i.test($elementid)){
    //My code
   }
}

Another issue i encounter was var $elementid = active.item.get(0).id; passed my element id value to $elementid. However

if(page.language() === 'FR' && /myid/i.test($active.item.get(0).id)){
        //My code
       }

The above failed and always return false. When i log active.item.get(0).id on the console, it turns out it returns an object not my id . This i am not sure though , looking for a better explanation as well.

Upvotes: 0

Magus
Magus

Reputation: 15124

It seems like active.item is not a string at all. So /topMenuBetaInterface/.test(active.item) will always be false.

It also explain why you get an error when you try active.item.indexOf("topMenuBetaInterface"). That because there's no indexOf function in active.item.

Upvotes: 1

Related Questions