-2

I have a python file which contains tables (coded in html). I wish to click on the cells of the table and (using the onclick feature of html) output the table value directly onto a textbox. I am able to do this in an html file but am struggling to incorporate the same model into a python file. The (working) code to copy the values of cells of a table to an alertbox is below:

http://jsfiddle.net/8A37s/5/ JavaScript:

var table = document.getElementById("tableID");

if (table != null) {

for (var i = 0; i < table.rows.length; i++) {

    for (var j = 0; j < table.rows[i].cells.length; j++)

    table.rows[i].cells[j].onclick = function () {

    tableText(this);

    };

}

}


function tableText(tableCell) {

alert(tableCell.innerHTML);

}

To incorporate html into this python file, I've used the following procedure (successfully):

print """<table>"""

Is the process for implementing javascript the same?

3
  • 1
    What do you mean "within"? Do you want to be able to import a JS compiler directly in the Python code? Commented Jun 30, 2017 at 18:42
  • I want to use both javascript and html in a python file. I am able already to use html in the python file, and I have a working javascript program, but I would like to modify the html code inside the python file using javascript (like how you normally do inside of an html file.) Commented Jun 30, 2017 at 18:47
  • stackoverflow.com/questions/3169781/printing-html-in-python-cgi this is the process by which I have been able to incorporate html into my python file Commented Jun 30, 2017 at 19:04

1 Answer 1

0
  • python syntax is just a words unless be read by python interpreter
  • html syntax is just a words unless be read by html interpreter (a webbrowser)
  • javascript syntax is just a words unless be read by javascript interpreter (a webbrowser)

Knowing this, you can mix them all in one file, but all must be understood by each interpreter, so before it reaches webbrowser treat html and js as string inside python.

so in python file you can:

your_js = """
var table = document.getElementById("tableID");

if (table != null) {

for (var i = 0; i < table.rows.length; i++) {

    for (var j = 0; j < table.rows[i].cells.length; j++)

    table.rows[i].cells[j].onclick = function () {

    tableText(this);

    };

}

}


function tableText(tableCell) {

alert(tableCell.innerHTML);

}
"""
print "<b>html</b>{}".format(your_js)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was able to incorporate this answer and it worked.

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.