jQuery event affecting only this instance of the element

Julian Samarjiev

I have more than one element with the same class name, ideally they will be seven, so, on pageload they will be hidden and then I want to show the extra text when clicking on the anchor, but only for that very instance of the div, my jQuery code is currently removing hidden in all instances of the div, I am guessing the solution would be to add this somewhere. Thanks!

$(".show-more").click(function() {
  return $(".extra-text").toggleClass("hidden");
});
.hidden {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
  <p>Text</p>
  <p class="hidden extra-text">More text</p>
  <a href="#" class="show-more">Show more..</a>
</div>
<div>
  <p>Text</p>
  <p class="hidden extra-text">More text</p>
  <a href="#" class="show-more">Show more..</a>
</div>

Rory McCrossan

You need to traverse the DOM to find the .extra-text element related to the one which raised the event.

To do this you can use the this keyword which will refer to the element that raised the click event. Try this:

$(".show-more").click(function() {
    $(this).closest('div').find(".extra-text").toggleClass("hidden");
});

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

JQuery - Keep the same event [mouseenter] in a nested element

分類Dev

jQuery element stored with `click` event not working as required

分類Dev

jQuery - Add click event to a dynamically created element

分類Dev

jQuery on, add only one event listener

分類Dev

jQuery recursive script only works on the first element

分類Dev

delete from datatable without affecting checkbox event

分類Dev

Fire click event for only one element with same class name

分類Dev

Execute Change event only if element has a specific attribute

分類Dev

JS trigger only affecting parent and siblings

分類Dev

jQuery `[jQuery created Element].is(":hover")` Only Seems To Work In Chrome

分類Dev

How to check if an jQuery event is bound to an element from a cypress test

分類Dev

Trigger an event when scroll to specific element and then stop it with jquery

分類Dev

Show Only One DIV Open on Click Event with Same Class jQuery

分類Dev

jQuery UI modal box affecting page styles

分類Dev

How to hide HTML element containing only spaces using jQuery?

分類Dev

Autofac Single Instance with event handlers

分類Dev

Bind event to wrapper element

分類Dev

Dynamically created checkbox click event selecting the whole row instead of only the check box in a jquery datatable. Why is that?

分類Dev

jquery click event to add/remove class to only one div, when several have the same classes

分類Dev

How to create an instance for Floating only?

分類Dev

nested element firing parent event

分類Dev

Onclick event not working - jQuery

分類Dev

Add event delegation in jquery

分類Dev

Cannot stop JQuery event

分類Dev

jQuery onClick Event not firing

分類Dev

Event is not firing in jQuery

分類Dev

jQuery selector for target event?

分類Dev

JQuery on click event on Href

分類Dev

jQuery 'click' event not firing

Related 関連記事

  1. 1

    JQuery - Keep the same event [mouseenter] in a nested element

  2. 2

    jQuery element stored with `click` event not working as required

  3. 3

    jQuery - Add click event to a dynamically created element

  4. 4

    jQuery on, add only one event listener

  5. 5

    jQuery recursive script only works on the first element

  6. 6

    delete from datatable without affecting checkbox event

  7. 7

    Fire click event for only one element with same class name

  8. 8

    Execute Change event only if element has a specific attribute

  9. 9

    JS trigger only affecting parent and siblings

  10. 10

    jQuery `[jQuery created Element].is(":hover")` Only Seems To Work In Chrome

  11. 11

    How to check if an jQuery event is bound to an element from a cypress test

  12. 12

    Trigger an event when scroll to specific element and then stop it with jquery

  13. 13

    Show Only One DIV Open on Click Event with Same Class jQuery

  14. 14

    jQuery UI modal box affecting page styles

  15. 15

    How to hide HTML element containing only spaces using jQuery?

  16. 16

    Autofac Single Instance with event handlers

  17. 17

    Bind event to wrapper element

  18. 18

    Dynamically created checkbox click event selecting the whole row instead of only the check box in a jquery datatable. Why is that?

  19. 19

    jquery click event to add/remove class to only one div, when several have the same classes

  20. 20

    How to create an instance for Floating only?

  21. 21

    nested element firing parent event

  22. 22

    Onclick event not working - jQuery

  23. 23

    Add event delegation in jquery

  24. 24

    Cannot stop JQuery event

  25. 25

    jQuery onClick Event not firing

  26. 26

    Event is not firing in jQuery

  27. 27

    jQuery selector for target event?

  28. 28

    JQuery on click event on Href

  29. 29

    jQuery 'click' event not firing

ホットタグ

アーカイブ