Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't manage to target an element in jQuery

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.

like image 999
Melky Avatar asked Nov 26 '18 09:11

Melky


People also ask

How do I target the right elements in jQuery?

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:

What is the event target in HTML?

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.

How to get the best performance using jQuery?

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:

What is the target property of an 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.


Video Answer


2 Answers

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.

like image 104
Colin Cline Avatar answered Oct 13 '22 05:10

Colin Cline


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>
like image 36
Ankit Agarwal Avatar answered Oct 13 '22 05:10

Ankit Agarwal