0

I have a div having many tags inside. I want to bind the click event on all text elements that have x attribute.

I have tried

$('#attendacneLineChart').find('text')

but this returns all the text elements. But I need only those text element that has x attribute.

3
  • 1
    It's not a duplicate. I mean clearly OP can select by attribute using jQuery, but they should never do this in angular. Instead this should be done with a directive. Commented Jul 12, 2016 at 9:58
  • 1
    There is no such thing as a 'text element,' can you please give an example of the HTML you're trying to describe? And what attribute are you looking for? What do you want to do on the 'click' event? Commented Jul 12, 2016 at 9:59
  • It is marked jQuery. Duplicate of this then? stackoverflow.com/questions/11913841/… - I closed it and then I saw the "text with attribute" which does not make sense anyway. OP needs to show examples of html with or without the attribute he meant Commented Jul 12, 2016 at 10:00

3 Answers 3

2

use

$('#attendacneLineChart').find('text[X]')

if you want to ignore some elements with specific attr you can use :not

$('#attendacneLineChart').find('text[X]:not([y])') // for more use 'text[X]:not([y]):not([z])'
Sign up to request clarification or add additional context in comments.

2 Comments

No, this is totally wrong. Remember, we are talking able Agnular, not jQuery.
thanks ! this is working.. If I want to ignore some elements with specific attribute ?
2

I want to bind the click event on all text elements that have x attribute.

Create directive for this:

.directive('x', function() {
  return {
    link: function(scope, element) {
      element.click(function() {
        console.log('clicked')
      })
    }
  }
})

Comments

0

You can use jquery [attribute='value'] selector to finding element has specific attribute.

$("div > p[myAttr]").click(function(){
    console.log($(this).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
    <p>Text1</p>
    <p myAttr="1">Text2</p>
    <p>Text3</p>
    <p myAttr>Text4</p>
</div>

Comments

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.