Member Avatar for feoperro

Hi,

I'm creating a table with X amount of rows (Importing from a Database table). I need to be able to click on a row in the table (any specified row), and onClick, the row number that was clicked on must be printed. Can anyone give me some advice on how to do this please?

Thanks a lot,
-Ashton.

Recommended Answers

All 7 Replies

Where do you want to be printed?
I would suggest to have a button with each row that has that "onclick" event.
When the rows are dynamically created you can have the value you want to be printed passed as a parameter at a method in the "onclick" event.
I don't if you can just click the row. Check this site to see if the
<tr> tag has an "onclick" event:

Javascript
HTML

Member Avatar for feoperro

Hi,

I finally managed to find something. Here is the source:

<script type='text/javascript'>
            function getRow(t){
            alert(t.parentNode.rowIndex);
            //var row=t;
            //document.vSOForm.rowNumber.value = row;");
            }
            </script>

Then in the table, use it as follows:

<td onclick='getRow(this)'>

The problem I'm having now, however, is that I can't seem to pass the row number to a variable; and if I try to pass it to an input box using the lines that I commented out:

//var row=t;
//document.vSOForm.rowNumber.value = row;");

Then it places the string:
"[object]" into the input field.

If I use parseInt:

document.vSOForm.rowNumber.value = parseInt(row);

Then it places the string:
"NaN" into the input field.

Is there an alternative method of returning an integer value to the input field?

How about a way of sending the javascript row variable to a JSP Scriptlet variable?

Thanks again,
-Ashton.

Not relevant to Java Server Pages, moving to JavaScript

Member Avatar for feoperro

Oops, nevermind, I fixed it!

<script type='text/javascript'>
            function getRow(t){
            alert(t.parentNode.rowIndex);
            //var row=t;
            //document.vSOForm.rowNumber.value = row;");
            }
            </script>

Should've been:

out.println("<script type='text/javascript'>");
            out.println("function getRow(t){");
            out.println("alert(t.parentNode.rowIndex);");
            out.println("var row=t.parentNode.rowIndex;");
            out.println("document.vSOForm.rowNumber.value = parseInt(row);");
            out.println("}");
            out.println("</script>");

Thanks anyway! Hope this thread helps someone else!

What exactly are you trying to accomplish?

Member Avatar for feoperro

Well, I want to get select all rows from a specific database table to a JSP page. Then I want the JSP page to auto-generate an html table with all the data from the resultset. Once the data is displayed, I want to be able to click on any of the rows on the html table and get the value for the row number I clicked on so that I can reference an ArrayList of javabeans which contain each cell's data that will be sent to separate input fields that have Add/Update/Search functions, that require the text in the text input areas. Wow what a mouthful.

hi anyone tell me an idea how to insert multiple rows in to database.
Dynamically i add row and after adding rows i need to insert into database.I am using jsp and servlet .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.