locked
button click handler not working RRS feed

  • Question

  • User-431110550 posted

    I am trying to attach the ExportPDF to the button exportSelectedPdf on a cshtml file but when I click the button the handler is not working. Any ideas why ?

    <input id="exportSelectedPdf" type="button" value="Export to PDF" />

    <script type="text/javascript" language="javascript">
    function ExportPDF() {
    alert("Hi");
    var url=@Url.Action("ExportTransactionsAsPdf", "JournalController")
    ;
    exportSelectedTransactions(url);
    }
    document.getElementById("exportSelectedPdf").addEventListener("click", ExportPDF());
    </script>

    Thursday, September 28, 2017 2:21 PM

All replies

  • User-1508012637 posted

    Hi AspDeveloperLife,

    There're several places need to be changed in your code,

    1. The @Url.Action("ExportTransactionsAsPdf", "JournalController") method returns a string without the double quotation, so please add them.

    2. The following statement execute the ExportPDF function directly:

    document.getElementById("exportSelectedPdf").addEventListener("click", ExportPDF());

    Acctually we don't need to add "()" which would let to the function to be executed, so please change you code as below:

    <input id="exportSelectedPdf" type="button" value="Export to PDF" />
    
    <script type="text/javascript" language="javascript">
        function ExportPDF() {
            alert("Hi");
    
            var url = "@Url.Action("ExportTransactionsAsPdf", "JournalController")";
    
            exportSelectedTransactions(url);
        }
        document.getElementById("exportSelectedPdf").addEventListener("click", ExportPDF);
    
    </script>

    If you have any other questions, please feel free to contact me any time.

    Best Regards
    Velen

    Friday, September 29, 2017 6:07 AM