logo
down
shadow

Get the closest ancestor from current event in Javascript


Get the closest ancestor from current event in Javascript

By : Sakura Lyra
Date : November 19 2020, 03:01 PM
fixed the issue. Will look into that further It looks like you're calling event.closest, which doesn't seem right. Did you mean to do element.closest, or perhaps event.target.closest?
code :
function handleClick(event) {
  const tr = event.target.closest('tr');
  tr.style.backgroundColor = 'red';
}


document.querySelectorAll('td').forEach(td => 
  td.addEventListener('click', handleClick));
td{border:1px solid blue}
<table>
  <tr>
    <td>Click</td>
    <td>me</td>
  </tr>
</table>


Share : facebook icon twitter icon
How do I select a closest ancestor plus the ancestor's next?

How do I select a closest ancestor plus the ancestor's next?


By : Eric W
Date : March 29 2020, 07:55 AM
help you fix your problem I'm using jQuery to manipulate table rows that are paired within a table. I'm trying to select rows based on found items from my previous jQuery where if the row contains a visible image, I need that row and it's next sibling. I can get the row using:
code :
var temp;
temp = $('whatever').closest('tr').add( temp.next() )
var temp = $('whatever').closest('tr'); temp = temp.next().andSelf()
How to find the 2nd closest ancestor in jQuery?

How to find the 2nd closest ancestor in jQuery?


By : user2791330
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further My DOM looks something like this: , Go up a parent:
code :
$(this).closest('li').parent().closest('li').addClass('selected');
$(this).parents("li:eq(1)").toggleClass("selected");
<li>
   <ul>
      <li><a class="editEntity>Edit</a></li>
      <li><a class="deleteEntity>Delete</a></li>
   </ul>
</li>
in javascript, if an event trigger is declared on an HTML element and its ancestor, which fires first?

in javascript, if an event trigger is declared on an HTML element and its ancestor, which fires first?


By : user3361442
Date : March 29 2020, 07:55 AM
wish helps you The input's handlers should be triggered first and then the event should bubble up the DOM hierarchy if it bubbles. From the DOM Events specification:
querySelector to select closest ancestor without jQuery

querySelector to select closest ancestor without jQuery


By : Tim
Date : March 29 2020, 07:55 AM
it should still fix some issue You're almost there, but if you don't want the element you initially selected to be a possible result, you'll have to call closest on its parent, not on the selected element.
code :
console.log(document.querySelector("#myselector").parentElement.closest(".card").id);
.card {
  background: green;
}
<div class="card">
  inside first
  <div class="card" id="tryingtoselectthis">
    inside second
    <div class="card" id="myselector">
      inside third
    </div>
  </div>
</div>
How to use XPath to find the closest ancestor in a ancestor-or-self nodelist

How to use XPath to find the closest ancestor in a ancestor-or-self nodelist


By : ciprianmp
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I think you get an ordered set of nodes, from parent to ancestor.
Try $currentNode/ancestor-or-self::a[1] to get parent of c att="val".
Related Posts Related Posts :
  • Bootstrap DatePicker format mm/yyyy set max month
  • Firebase cloud firestore + auth: write only for signed in users
  • Getting jQuery.data functionality without jQuery
  • How to get incrementing serial numbers for new and removed items for jQuery sortable?
  • Highlighting a word or sentence in iframe, using javascript/Jquery
  • Calculate what percentage of a specific element has been scrolled into view
  • Knockout autocomplete with jquery doesn't allow to select custom value
  • react native - react-native-maps performance slow on iOS
  • ajax fallback when no internet connection
  • Show Textbox based on RadioButton selection or value when Page Loads
  • JS maximum call stack exceeded
  • Resetting object key values
  • How can i disable the toggle, preventing user to tap/click it?
  • How to include javascript file into LOV popups on apex oracle?
  • Javascript Angular 4 eventEmitter with ngClass
  • Webpack - module not found even though module exists
  • How to display following values using vue js?
  • Regexp: Allow only use of a few words and only once per word
  • CCapture.js webm video blacked out
  • Using a HTML hyperlink to call a JS function on the parent element
  • Return undefined from existing property in javascript model
  • What is the Difference Between These two jQuery Code Snippets?
  • How to get Network Speed in WebRTC
  • How to get text from selected value in a dropdownlist which is js based
  • window is not defined angular universal third library
  • Angularjs ng-repeat stylization depending on previous value
  • Trying to implement Fittext.js
  • Calculate number of match in array Lodash
  • Jquery Smooth Scroll Using Offset.top
  • How to extract data to React state from CSV file using Papa Parse?
  • How to add unique links to google maps markers
  • How to use if condition in a tool bar in java script
  • Ajax filter in django not showing in HTML
  • data collection with Javascript
  • Rotate image on lightbox2 load
  • Prevent body from scrolling when a Pop-Up is open
  • How to copy files that do not need to be compiled in Gulp?
  • Array not assigned to variable? How does this work and what exactly is it doing?
  • Sorting associative array of objects in javascript
  • Changing Icon in Sap.m.tree having CustomTreeItem
  • Merge two array of objects based on a key
  • javascript in css not working
  • Passing only clicked element to onClick function - reactjs
  • React boilerplate doesn't load js files in the index.html
  • is Child service inside child component visible in the Parent component?
  • Check if data attribute value equals a string
  • How to get value of child tag of a button tag
  • How to access subjects of selected mails in Apple Mail using JavaScript?
  • How to get all dynamically set inline-style CSS in jQuery?
  • Error: Module "html" does not provide a view engine (Express)
  • Random Image in <Div> from array
  • Slider with touch function
  • ReactJS Component Architecture Problems / Nested Components or Single Component Manager
  • Javascript: Caching within Closure doesn't work
  • HTM5 Canvas Drawing App: How Do I Select The Color?
  • Assigning Events using HTML DOM
  • html5 getUserMedia() portrait mode
  • How to avoid 'headers already sent' within Promise chain?
  • Get a result from a react native app integrated into an existing android app
  • Why does the value of input field return undefined
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org