1
<a class="contacts" ng-click="contactSelect(contact.userID)">
     <!---->
     <span ng-switch-default="" my-user-status="::contact.userID">Hello World !</span>
     <!---->
     </div>
</a>

I'm using this code for click: $('.contacts').click(); and it is working, but my problem is I want to click on all tags a that have string Hello World !.

charlietfl
  • 170,828
  • 13
  • 121
  • 150
Negar
  • 19
  • 5
  • The `:contains()` selector in jQuery may solve your problem: https://api.jquery.com/contains-selector/ – Jon Uleis Dec 18 '16 at 01:42
  • Not at all clear what you are asking or why you need jQuery when you already have `ng-click`. This is an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) What does `contactSelect()` do and how is it determined what string is used? Please take a few minutes to read through [ask] – charlietfl Dec 18 '16 at 01:44
  • Strongly suggest you read through [Thinking in angular when I have a jQuery background](http://stackoverflow.com/q/14994391/1175966) – charlietfl Dec 18 '16 at 01:51
  • @JonUleis Thanks a lot – Negar Dec 18 '16 at 01:51
  • @charlietfl Thanks – Negar Dec 18 '16 at 01:57
  • This jQuery approach is all wrong and could be fixed using angular only if you are interested. Would need a bit more detail about what your ng-switch works from – charlietfl Dec 18 '16 at 01:59

2 Answers2

0

You should be able to do whatever it is you want to do not by focusing on the string inside the ng-switch element but by the property that changes what ng-switch displays and do everything inside your ng-click and get rid of using jQuery for this.

Change ng-click to pass in the whole contact object, not just the userID

<a class="contacts" ng-click="contactSelect(contact)">

Then in controller I will assume that the ng-switch is related to the contact object:

var switchValues =['one','two','three'];// if not in this array will use default
$scope.contactSelect = function(contact){
   var userId = contact.userId;
   // do something with the userId

   var switchValue = contact.switchProperty;
   // do something with the switchProperty
    if(switchValues.indexOf(switchValue ) === -1){
       // this will be default case with string "hello world" in the dom
       // do whatever you would have done in the jQuery
    }

}

This is very rough since not much detail was provided in the question

charlietfl
  • 170,828
  • 13
  • 121
  • 150
-1
$(".contacts").click(function(e){
    if($(this).find("span").text() == "Hello World !"){
        e.preventDefault();
        // Do what you need to do here.
    } else {
        // String didn't match.
    }
});

or as user @Jon Uleis reference, you can use jQuery's ":contains()" selector like this:

$(".contacts:contains('Hello World')").click(function(e){
        e.preventDefault();
        // Do what you need to do here.
});
radiantstatic
  • 342
  • 4
  • 20
  • no need for jQuery in an angular app for this. This is simply promoting bad practices – charlietfl Dec 18 '16 at 01:46
  • @charlietfl - because his question specifically used jQuery? – radiantstatic Dec 18 '16 at 01:47
  • But it is also using angular ... which has a more specific data first approach and your code will conflict with `ng-click`. OP is asking wrong question – charlietfl Dec 18 '16 at 01:48
  • @charlietfl - that I am not disputing. However, regardless of the additional context provided (ie. the code snippet with angular) - his question was asking about extending his jQuery selector. So, yeah... – radiantstatic Dec 18 '16 at 01:51
  • Doesn't make the answer right when it promotes incorrect practices. This is after all a "professional programming" site – charlietfl Dec 18 '16 at 01:52
  • @charlietfl - that just doesn't make any sense at all. A solution can solve a problem while also not being a "best practice". To reiterate, the question was asking about extending his jQuery selector. But i'll stop now and let you continue on your quest. – radiantstatic Dec 18 '16 at 01:55
  • Well my point is that providing the proper answer using angular methodology would do a lot more to help teach OP what is the proper way to handle such an issue. jQuery has no business being used here. You also should learn what an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) is – charlietfl Dec 18 '16 at 01:57
  • Since I did give you a bit of a hard time, I also posted a answer to reflect a rough angular approach based on the very limited details in question – charlietfl Dec 18 '16 at 03:21