score:0
many ways to get it..
fews are..
using parents()
$('.products a span').click(function(){
alert($(this).parents('.products').find('h4').text());
});
using closest()
.
$('.products a span').click(function(){
alert($(this).closest('.products').find('h4').text());
});
using siblings()
$('.products a span').click(function(){
alert($(this).parents('.buyatdealer').siblings('h4').text());
});
score:0
I would try to re-think the problem you're trying to solve, and then the solution.
What happens when someone clicks "BUY ONLINE" that you need the text for? Are you using it for presentation, or is this part of the actual purchase process, sent off to the backend (or something similar)?
Trusting the text in an h4
closest to a "BUY ONLINE" button seems error-prone.
As an alternative to this DOM traversal and strange coupling, could you instead set a data-attribute on the "BUY ONLINE" button?
<span data-product-id="0" data-product-name="Product One">BUY ONLINE</span>
... or even better ...
<button data-product-id="0" data-product-name="Product One">BUY ONLINE</button>
:)
score:0
$(this).closest(".products h4").text(); would get you that text, assuming you always keep the h4 tag as a child of .products.
score:4
Asuming you are on the click function and clicked the a
.
$(this).closest(".products").find('h4').text();
Related Query
- Selecting the anchor tag of a particular href using jQuery
- jQuery selector: selecting the body tag of an iframe
- jQuery: Limit the scope of a jQuery selector to avoid selecting other selectors that are not children
- how to make a form pop up on the same page after clicking an anchor tag with jquery
- Dynamically created checkbox click event selecting the whole row instead of only the check box in a jquery datatable. Why is that?
- how to add certain number of blank colums to the right of html table using jquery
- jQuery Mobile does not move to the next page after clicking on anchor tag
- jQuery Move a fixed or absolutely positioned element to the left or right
- jQuery check the right checkbox inside a TD
- swich off others element with the same selectors and switch on the clicked one in jquery
- In Chrome, while selecting the selectbox the jquery event is not fired
- Live search results as you type... am I going about this the right way? jQuery + PHP
- jQuery window.on resize not firing at the right time
- Maximum number of characters allowed per tag in the jQuery UI plugin Tag-it
- Can't get selectors to work with the JQuery SVG library
- insert text .before() INSIDE the tag with jquery
- Trim blank spaces from the right in jQuery
- simplest jquery to have div "position:fixed" to the bottom right corner(always.. like on scrolling down)?
- jQuery bxSlider does not start at the right position
- jQuery - selecting every .class member except the parent of $(this)
- How to use jquery selectors to get the third or fourth element and set its id
- how to change an anchor tag using jquery by selecting its class
- How can I run code right before the jQuery submit() executes?
- How can I prevent jquery from selecting a parent when I click on one of the children in that parent?
- Can the body tag exist inside of a jQuery object?
- Get the right var config when selecting a value
- JQuery set the same event for several selectors
- The right way to remove a class with jQuery
- jQuery selecting the 3rd, 4th and 5th TD in every row with ID
- Get the real clicked element tag name for once with jquery
- What is the correct way of selecting drop down text in jQuery starting from a div?
- jQuery selector is only selecting the first link
- How do you call the tabselect event when selecting a jquery ui tab programmatically
- jquery selecting sibling node of the current node
- How do the performance characteristics of jQuery selectors differ from those of CSS selectors?
- Selecting multiple selectors with Jquery
- Change the background color of an html tag from a dropdown menu with jquery
- selecting an element one before the previous from selected option with jQuery
- How to replace the HTML within a label tag using jQuery
- get the string enclosed in a tag in a string Jquery
- jquery code for getting the href of 'a' tag near the button that is being clicked
- jQuery mobile listview - add some text on the right
- Getting the jquery selector right
- Using jquery How can I remove the anchor tag <a> but keep the contents?
- how to change the value in the html tag using jquery
- Is there a Jquery plugin to replace the select tag which lets users type in their own option?
- Looking for the right naming of "big navigation" and also some demo examples with jQuery
- How can i add a css class only to the h3 tag clicked using jQuery
- jQuery - Trigger event if a horizontally-scrollable div is at the left most and right most of scroll
- Selecting the right children of a class
More Query from same tag
- div - createElement is not working?
- Is it possible to add elements to React's Virtual DOM but use jQuery for append to the real DOM?
- jQuery sortable list - disable first element
- Display JSON data into HTML with jQuery
- Deleting the full object despite the redefinition
- How to create a List<> hidden field and submit it to controller in MVC
- Searching for a Greek letter in a string Javascript
- Angular chosen error with undefined is not a function
- help on jQuery working trying to autoFocus on the next input field after keyUp, being as generic as possible
- .mousedown() event not triggering on JScrollPane when clicking on the dragbar
- Adding line breaks to absolutely positioned text blocks over images to create strips of text
- Bootstrap image gallery always opens at top of page
- Convert comma separate string to Key value object jQuery
- jQuery jPicker reset completely
- Kendo UI Web - DropDownList: select event doesn't return selected value properly
- Disqus comments are overlapping my footer
- Jquery Autocomplete clear textbox if no match found
- Ajax: Why is the post request fired twice?
- Can't display the selected item in the text input field with twitter-bootstrap typeahead
- click requests using mootools
- if index of div is equal to index of element
- How do I implement a dynamically changing grid on a web page?
- MVC Razor Ajax Form Submit giving 'unexpexted token u'
- DataTables warning (table id = 'IDTableName'): Cannot reinitialise DataTable
- JQuery not recognizing class
- Having trouble posting jquery onto php
- How to disable submit button while form is processing and reenable once finished?
- jQuery how to use DOM objects from external source
- Using a default name for autoplay through jQuery
- Tampermonkey JavaScript jQuery how to find and edit elements of an embedded web page?