0

I have the following basic html table with a Java function to print only the table content However the Print button does nothing when clicked,I figure probably the java coding is the issue, however it is not working could I get a third pair of eyes to assist please

<html>
<body>

<table border="1" cellpadding="3" id="printTable">
    <tbody><tr>
        <th>First Name</th>
        <th>Last Name</th>      
        <th>Points</th>
    </tr>
    <tr>
        <td>Jill</td>
        <td>Smith</td>      
        <td>50</td>
    </tr>
    <tr>
        <td>Eve</td>
        <td>Jackson</td>        
        <td>94</td>
    </tr>
    <tr>
        <td>John</td>
        <td>Doe</td>        
        <td>80</td>
    </tr>
    <tr>
        <td>Adam</td>
        <td>Johnson</td>        
        <td>67</td>
    </tr>
</tbody></table>

<br />
<br />

<button>Print me</button>

<script>
function printData()
{
   var divToPrint=document.getElementById("printTable");
   newWin= window.open("");
   newWin.document.write(divToPrint.outerHTML);
   newWin.print();
   newWin.close();
}

$('button').on('click',function(){
printData();
})
</script>
</body>
</html>
5
  • what is the issue you're having? Commented Apr 13, 2015 at 16:56
  • @Paco the print button does nothing when I run the code Commented Apr 13, 2015 at 16:57
  • Do you understand the code you posted? If so, you should edit your post explaining exactly what it is supposed to do. What do you expect to happen? and so on Commented Apr 13, 2015 at 16:59
  • @Paco Yes The table has an ID="printTable" which refers to the java function "PrintData", and when the button is clicked it shoud print only the table with the PrintTable Id, however Nothiong happens when the button is clicked, and thats what I am seeking help with Commented Apr 13, 2015 at 17:03
  • Remove outerHtml & instead try document.getElementById("printable").innerHTML Commented Apr 13, 2015 at 17:03

3 Answers 3

2

When working with javascript, it is often useful to look at your browser's console. After opening this page, mine has the error:

ReferenceError: $ is not defined

$ is a global variable inserted by jQuery. Try adding

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

to the code. This will include the jQuery library.

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

1 Comment

Be sure to add it before $ is used - right after <button>Print me</button> would be a good place.
1

The call to the javascript function is not correct. When you're using the '$' symbol it means that you're using JQuery. You have 2 options:

1 - Include the jquery library to your page:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>

2 - Call the javascript function directly on the button on the 'onclick' event. This is the best option in your case:

<button onclick="printData()">Print me</button>

Comments

1

Change

var divToPrint=document.getElementById("printTable");

to

var divToPrint=document.getElementById("<%=printTable.ClientID>");

and add jquery library

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

Comments

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.