EasyE
EasyE

Reputation: 580

How to bind link to a button which I can manipulate with Jquery

currently I am running a process which with return a link to me on my html, however I would like a button instead of a link, in order for me to grab it with Jquery.

Javascript

                    var link = document.createElement("a");
                if (link.download !== undefined) { // feature detection
                    // Browsers that support HTML5 download attribute
                    link.setAttribute("href", uri);
                    link.setAttribute("download", fileName);
                }
                else if (navigator.msSaveBlob) { // IE 10+
                    link.addEventListener("click", function (event) {
                        var blob = new Blob([buffer], {
                            "type": "text/csv;charset=utf-8;"
                        });
                        navigator.msSaveBlob(blob, fileName);
                    }, false);
                }  

                //$("ResultButton").click(function () {
                  //  $("p").hide(1000); 
               //}); << this block was removed after testing

                //HTMLButtonElement.innerHTML = "<input type = 'button' value = 'Export to CSV'>";
                link.innerHTML = "Export to CSV";
                document.body.appendChild(link);

I would like to somehow bind it with my ResultButton in order for me to have it hidden and then show it after my action is complete

HTML

<button id="ResultButton" style="display: none;"> Return Results</button>

Upvotes: 0

Views: 60

Answers (2)

Pramus
Pramus

Reputation: 430

Create the button as hidden and once you want to show it run $('#ResultButton').show()

EDIT: Or you can also create a new element without even using jQuery

<button onclick="myFunction()">Try it</button>

<script>
    function myFunction() {
    var btn = document.createElement("BUTTON");
    var t = document.createTextNode("CLICK ME");
    // some other optional attributes //
    btn.appendChild(t);
    document.body.appendChild(btn);
}
</script>

Upvotes: 0

Javish
Javish

Reputation: 140

Change the following line

$("ResultButton").click(function () {

to

$(“#ResultButton").click(function () {

‘#’ denotes that you are using ID of an element.

Upvotes: 1

Related Questions