kst
kst

Reputation: 1518

Change JS function to jQuery

How can I change this code to take advantage of jQuery?

function download(elm) {
    var iframe = document.createElement("iframe");
    var param = elm.innerHTML; //$get("filedownload").innerHTML;        
    //iframe.src = "GenerateFile.aspx?filename=386c1a94-fa5a-4cfd-b0ae-40995062f70b&ctype=application/octet-stream&ori=18e73bace0ce42119dbbda2d9fe06402.xls";// + param;
    iframe.src = "GenerateFile.aspx?" + param;

    iframe.style.display = "none";

    document.body.appendChild(iframe);
}

Upvotes: 3

Views: 290

Answers (1)

Nick Craver
Nick Craver

Reputation: 630389

It would look like this:

function download(elm) {
  $("<iframe />", { src: "GenerateFile.aspx?" + elm.innerHTML })
    .appendTo("body").hide();
}

This is the jQuery 1.4+ $(html, props) syntax, for older versions it would look like this:

function download(elm) {
  $("<iframe />").attr("src","GenerateFile.aspx?" + elm.innerHTML)
    .appendTo("body").hide();
}

Past the creation .appendTo() appends the element you created to the selector passed in ("body"), and .hide() covers the display: none; styling.

Upvotes: 10

Related Questions