1

I want to assign a javascript function to an HTML attribute. for eg:

 <li data-ng-repeat="job in jobList" class= foo(str) data-filter = "foo1(str)">

i want to do this because the class name and data attribute changes by job. finding the <li> element and changing is not possible as it is a list of elements.

i tried using

class=foo(str)
class = javascipt'foo(Str);'

not able to get it right.

2
  • Are there supposed to be quotes surrounding foo(str) after class=? Edit: You must include quotes around foo(str) otherwise it is NOT valid HTML. Commented Jun 13, 2016 at 22:24
  • Does data-ng-class not work? Commented Jun 13, 2016 at 22:26

1 Answer 1

2
<li data-ng-repeat="job in jobList" class="{{foo(str)}}" data-filter = "foo1(str)">

However that will only work if foo is on the global namespace. Assuming you have put foo on the scope then the code is the following:

<li data-ng-repeat="job in jobList" class="{{scope.foo(str)}}" data-filter = "foo1(str)">

AngularJS will evaluate foo every time the DOM is drawn

Sign up to request clarification or add additional context in comments.

3 Comments

Keep in mind I am assuming you are familiar with and running AngularJS since you are using the data-ng-repeat tag. My solution requires AngularJS to be setup properly with a controller.
Damn.. how stupid of me.. i completely forgot that i can use angular.. somehow i was thinking of only typical onclick kind of solution. Thanks @samuel
Haha no problem Karty! :) Could you please go accept my answer so I can get that delicious reputation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.