0

The updateValue() method isn't firing and I'm not sure how to even debug this using the browser.

function generateHtmlTableRow() {
    var tr = $("<tr></tr>");
    $("#results").append(tr);

    var someTextData = "test";
    tr.append("<td><input type=\"button\" value=\"TestButton\" onclick=\"updateValue(someTextData);\" /></td>");
}

function updateValue(newText) {
    alert(newText);
}
6
  • 4
    Don't use inline JavaScript for your event handler. Use jQuery's .on() function with event delegation. Commented Mar 16, 2016 at 17:11
  • Also, if you use single quotes to wrap your string, no need to escape the double quotes. Commented Mar 16, 2016 at 17:12
  • 1
    Try this.. tr.append("<td><input type=\"button\" value=\"TestButton\" onclick=\"updateValue('"+someTextData+"');\" /></td>"); Commented Mar 16, 2016 at 17:13
  • You can debug using the browser console. Commented Mar 16, 2016 at 17:14
  • 1
    Sure. jsfiddle.net/j08691/3fpxqw1u Commented Mar 16, 2016 at 17:38

2 Answers 2

1

The generated html is the problem. It cannot reference a variable in the scope of the generateHtmlTableRow function. So it will work:

function generateHtmlTableRow() {
    var tr = $("<tr></tr>");
    $("#results").append(tr);

    var someTextData = "test";
    tr.append("<td><input type=\"button\" value=\"TestButton\" onclick=\"updateValue('" + someTextData + "');\" /></td>");
}

function updateValue(newText) {
    alert(newText);
}

$(document).ready(function(){
  console.log('log');
  generateHtmlTableRow();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="results"></div>

Sign up to request clarification or add additional context in comments.

2 Comments

This a bad example of solving such a problem, this is not how it should be done.
Thanks, I knew my syntax was off somehow!
1

An elegant way to do this is to

  • Store the someData values in HTML5 data- attributes when you create the <tr>. jQuery has the .data() function for this purpose.
  • Use a delegated event handler that catches all button clicks inside the <table>. The event handler can then retrieve the data again easily.

function generateTableRow(someData) {
  $("<tr><td><button class='test'>TestButton</button></td></tr>")
    .data("value", someData)
    .appendTo("#results");
}

$(function(){
  $("#results").on("click", "button.test", function () {
    var value = $(this).closest("tr").data("value");
    alert(value);
  });
  generateTableRow("test 1");
  generateTableRow("test 2");
  generateTableRow("test 3");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table id="results"></table>

2 Comments

+1 This is a more elegant way to implement but I chose @Meiko's answer as it was answering and explaining what I was doing wrong not a better way to do it. In the future I will refactor to use this instead however.
No worries, as long as you use it to learn it's all right. Thanks for the feedback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.