How to execut a javascript function in a php table -
I am developing a small web page
On this page I have a table and one A function is created that takes ID and gives an image
Everything is working correctly, the point is that whenever I click on the row that the function is executed.
I just wanted to execute the function when I click on the attached column of the ID. (Second column)
Can anyone help me?
Did you understand my doubts?
Thank you all.
& lt; Script & gt; Function addRowHandlers () {var table = document.getElementById ("tableId"); Var rows = table.getAllementsBigename ("TD"); {Var currentRow = table.rows [i] for (I = 0; i & lt; rows.length; i ++); Var createClickHandler = function (line) {return function () {var cell = row.getElementsByTagName ("td") [1]; Var id = cell.innerHTML; // ajax.php file $ .post ("ajax.php", {id: id}, function (data) {// add as a result and posting an ID to create a model / dialog popup similar to the warning Use AJAX for () $ ('& lt; div / & gt;') .html (data) .dialog ();}); }; }; CurrentRow.onclick = createClickHandler (currentRow); }} Window.onload = addRowHandlers (); & Lt; / Script & gt;
You are already using jQuery, so ... Use:
Prepare the code prepared on the document $ (function () {// "click" on the table hook, but only if you actually call the / / Click the line $ ("# table id"). ("Click", "tr td: nth-child (2)", function () {// post $ .post ("ajax.php", { Id: $ (this) .text () // & lt; == Get ID from receiving text of clicked cell}, Function (data) {// Add result to a div and create a model / dialog popup similar to alert. $ ('& Lt; div / & gt;') .html (data) .dialog ();});} );});
Contradictions:
-
We are preparing it on a document rather than window load, which is long ago.
-
Using an accredited handler instead of a handler on each table cell.
- Use
: To get the second
line, note that this Believe that you will have only cells on your line (99.9999% is true;td
in nth-child (2)template
is also allowed here, though). -
Using
$ (this) .text ()
toid
id
instead of $ Post
Call:
// Prepare the code prepared on the document $ (function () {// hook "click" on the table, but only actually calls the handler If, // click passes through a second cell in a line $ ("# tableid"). ("Click", "tr td: nth-child (2)", function () {warnings ( $ (This) .text ());});});
& lt; Script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" & gt; & Lt; / Script & gt; & Lt; Table id = "tableId" & gt; & Lt; Tbody & gt; & Lt; TR & gt; & Lt; TD & gt; Foo & lt; / TD & gt; & Lt; TD & gt; 23 & lt; / TD & gt; & Lt; TD & gt; Bar & lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; Foo & lt; / TD & gt; & Lt; TD & gt; 42 & lt; / TD & gt; & Lt; TD & gt; Bar & lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; Foo & lt; / TD & gt; & Lt; TD & gt; 67 & lt; / TD & gt; & Lt; TD & gt; Bar & lt; / TD & gt; & Lt; / TR & gt; & Lt; / Tbody & gt; & Lt; / Table & gt;
Comments
Post a Comment