I'm trying to target a parent from a link with a jQuery function, at first to get its innerHtml but now, just to get its value.
However, I can't manage to track it and I've been putting way too much time on this simple problem.
Here's my html :
<table>
<tr>
<td title="td_title_1" value="td_value_1">Some text<a href="#" title="Copy">Element_1</a></td>
<td title="td_title_2" value="td_value_2">Some other text<a href="#" title="Copy">Element_2</a></td>
</tr>
</table>
And my jQuery:
$(function(){
$("*").on('click','a[title="Copy"]',function(){
var clipboard = $(this).parent('td').find('[title="td_title1"]').val();
alert(clipboard);
});
});
I tried parent(), parents(), closest()... I'm still getting "undefined" value. And yeah, the jQuery lib is added.
Also, how would you get the "Some text" part?
Thanks for your time.
Fortunately, jQuery has some other tricks for targeting the right elements. jQuery uses CSS Selectors to target elements. The target:nth-child (n) CSS selector allows you to select all the nth elements with the target class or element type. Here's how you would give the third element in each well the bounce class:
Description: The DOM element that initiated the event. The target property can be the element that registered for the event or a descendant of it. It is often useful to compare event.target to this in order to determine if the event is being handled due to event bubbling.
In order to get the best performance using :enabled, first select elements with a standard jQuery selector, then use .filter ( ":enabled" ), or precede the pseudo-selector with a tag name or some other selector. Using the :input selector selects all <input>, <textarea>, <select>, and <button> elements:
The target property can be the element that registered for the event or a descendant of it. It is often useful to compare event.target to this in order to determine if the event is being handled due to event bubbling.
value
is not a valid property for td
or anything else but input. You had to use data-value
to be correct.
HTML:
<table>
<tr>
<td title="td_title_1" data-value="td_value_1">Some text<a href="#" title="Copy">Element_1</a></td>
<td title="td_title_2" data-value="td_value_2">Some other text<a href="#" title="Copy">Element_2</a></td>
</tr>
</table>
JS:
$(function(){
$(document).on('click','a[title="Copy"]', function(){
var clipboard = $(this).parent().data('value');
alert(clipboard);
});
});
*
(star selector) is a tricky selector. Do not use it in every purpose.
The first thing is you need to use stopPropagation()
so that it works only for the element you desire and the next thing is you can use the simple way to clone the element so that you get only the text part of <td>
element that is the immediate parent of the clicked element.
$(function() {
$("*").on('click', 'a[title="Copy"]', function(e) {
var clipboard = $(this).parent('td')
.clone() //clone the element
.children() //select all the children
.remove() //remove all the children
.end() //again go back to selected element
.text();
alert(clipboard);
e.stopPropagation();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td title="td_title_1" value="td_value_1">Some text<a href="#" title="Copy">Element_1</a></td>
<td title="td_title_2" value="td_value_2">Some other text<a href="#" title="Copy">Element_2</a></td>
</tr>
</table>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With