Change js function to jQuery

How can I change this code to use 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);
}
+3
source share
1 answer

It will look like this:

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

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

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

A past creation .appendTo()adds the element you created to the passed selector ( "body") and .hide()embraces the style display: none;.

+10
source

Source: https://habr.com/ru/post/1774918/


All Articles