How would I remove the $ marks from this JavaScript code?

I forgot how this is done.

Code: https://jsfiddle.net/3jr8otmg/1/

$(document).ready(function () {
    $("#btn").on("click", function () {
        var viewport = 640;
        if ("" != $("#input").val()){
            viewport = $("#input").val();
        }
        var str = $('#data').val();
        var result = str.replace(/(\d*\.\d+|\d+)px/gi, function (px) {
            var pointNum = parseFloat(px);
            var x = convertPXToVW(pointNum, viewport);
            return x + 'vw';
        });
        $('#result').val(result).select();
        document.execCommand("copy");

        $(".dasaochep").addClass("fadeIn"),
        $(".dasaochep").removeClass("fadeOut"),
        setTimeout(function () {
            $(".dasaochep").removeClass("fadeIn"),
            $(".dasaochep").addClass("fadeOut");
        }, 500);
    });
});
function convertPXToVW(px, width) {
    var a = px * (100 / width);
    return Math.round(a * 10000) / 10000;
}

Do you mean how do you convert jQuery to vanilla JS?

Detailed instructions on converting jQuery to JavaScript is found at:

2 Likes

This:

$(document).ready(function() {
  // code
})

Becomes: this

document.addEventListener('DOMContentLoaded', function() {
  // code
})

Yes that’s right. But, you have no need for DOMContentLoaded. Scripting code is run at the end of the <body>, which happens before the DOMContentLoaded event.

Using the DOMContentLoaded event delays the running of the code.
Removing that event results in even faster running code with no complications.

// document.addEventListener('DOMContentLoaded', function() {
  code
// })
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.