Using a HTML hyperlink to call a JS function on the parent element

63penguin

I have an HTML table as shown below. At the end of each non-header row, I have two links, one for copy and one for edit. When clicked, they should trigger a JavaScript function that is defined below.

function editRow(el) {
    //alert function added for debug purposes
    alert(el.rowIndex);
}
<table>
    <tr>
        <th>Date</th>
        <th>Tools</th>
    </tr>
    <tr>
        <td>20 October 2017</td>
        <td>
            <a href="javascript:editRow(this)">Edit</a>
            <a href="javascript:copyRow(this)">Copy</a>
        </td>
    </tr>
    <tr>
        <td>19 October 2017</td>
        <td>
            <a href="javascript:editRow(this)">Edit</a>
            <a href="javascript:copyRow(this)">Copy</a>
        </td>
    </tr>
</table>

However, the alert always says undefined. I think this is because the hyperlink is calling the function on the text and not the cell. How can I change the editRow(this) call to one that calls it on its parent element? I tried editRow(parent) and editRow(this.parent) but it didn't do anything useful.

Juan Mendes

Your problem is that when calling javascript using javascript: editRow(this), the this is set to the window object

You can fix your code by installing the handlers from HTML, but you really should do it from JS.

Added an example below using the onclick HTML attribute for simplicity.

function editRow(obj) {
   console.log('edit handler tagname', obj.tagName);
}

function copyRow(obj) {
   console.log('copy handler parent tr rowIndex', obj.parentElement.parentElement.rowIndex);
}
<table>
  <tr>
    <th>Date</th>
    <th>Tools</th>
  </tr>
  <tr>
    <td>20 October 2017</td>
    <td>
      <a onclick="editRow(this)" href="#">Edit</a>
      <a onclick="javascript:copyRow(this)" href="#">Copy</a>
    </td>
  </tr>
  <tr>
    <td>19 October 2017</td>
    <td>
      <a onclick="editRow(this)" href="#">Edit</a>
      <a onclick="copyRow(this)" href="#">Copy</a></td>
  </tr>
</table>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

call javascript function on hyperlink click

From Dev

react.js call parent function from child

From Dev

Call parent function onChange

From Dev

Parent element not getting child function call arguments

From Dev

How to call JS function on "hover" from HTML?

From Dev

angular 4 - call child component function from parent component html

From Dev

Call a function in imported child component on click from a parent element?

From Dev

Call a js function on an html snippet rendered using ngBindHtml

From Dev

Is there a way I can call a function for each html element when iterated using javascript loops

From Dev

Position HTML element relative to bottom of parent element using CSS?

From Dev

enable/disable html hyperlink of parent element from iFrame (For given structure) using jquery/javascript

From Dev

Obtain Parent of HyperLink In JS method when I click on HyperLink

From Dev

hyperlink / button with custom function call in jqgrid

From Dev

Call javascript function by clicking on HTML list element

From Dev

Jquery: call p element in parent function

From Dev

Call a javascript function from select element in HTML

From Dev

Is there a more generic way to call JS functions on an HTML element other than putting function calls against each element?

From Dev

Js function call with param on HyperLink

From Dev

Call a function with parameter in codeigniter using hyperlink

From Dev

call function from parent HTML

From Dev

Click on HTML element using JS

From Dev

Call function from parent (using new)

From Dev

Call a function if not an element was clicked in svg, but other elements using d3.js v5.4.0

From Dev

Cell Hyperlink in PHP using HTML

From Dev

How to set hyperlink as parent of a button using jquery?

From Dev

Call function in parent component without using props

From Dev

How to call an ID specifically within the parent element and ignore all other elements with the same ID (using pure JS)?

From Dev

Is there any difference in using React component as a (HTML tag)/ (JS function call)

From Dev

Call JS function in html on server side (nodejs)

Related Related

  1. 1

    call javascript function on hyperlink click

  2. 2

    react.js call parent function from child

  3. 3

    Call parent function onChange

  4. 4

    Parent element not getting child function call arguments

  5. 5

    How to call JS function on "hover" from HTML?

  6. 6

    angular 4 - call child component function from parent component html

  7. 7

    Call a function in imported child component on click from a parent element?

  8. 8

    Call a js function on an html snippet rendered using ngBindHtml

  9. 9

    Is there a way I can call a function for each html element when iterated using javascript loops

  10. 10

    Position HTML element relative to bottom of parent element using CSS?

  11. 11

    enable/disable html hyperlink of parent element from iFrame (For given structure) using jquery/javascript

  12. 12

    Obtain Parent of HyperLink In JS method when I click on HyperLink

  13. 13

    hyperlink / button with custom function call in jqgrid

  14. 14

    Call javascript function by clicking on HTML list element

  15. 15

    Jquery: call p element in parent function

  16. 16

    Call a javascript function from select element in HTML

  17. 17

    Is there a more generic way to call JS functions on an HTML element other than putting function calls against each element?

  18. 18

    Js function call with param on HyperLink

  19. 19

    Call a function with parameter in codeigniter using hyperlink

  20. 20

    call function from parent HTML

  21. 21

    Click on HTML element using JS

  22. 22

    Call function from parent (using new)

  23. 23

    Call a function if not an element was clicked in svg, but other elements using d3.js v5.4.0

  24. 24

    Cell Hyperlink in PHP using HTML

  25. 25

    How to set hyperlink as parent of a button using jquery?

  26. 26

    Call function in parent component without using props

  27. 27

    How to call an ID specifically within the parent element and ignore all other elements with the same ID (using pure JS)?

  28. 28

    Is there any difference in using React component as a (HTML tag)/ (JS function call)

  29. 29

    Call JS function in html on server side (nodejs)

HotTag

Archive