Search code examples
javascriptvelo

Javascript using Wix website builder


While using some code to get an image url, I have ran into some issues, the code is saying url.match is not a function.

Any help would be great!

function youtube(url = $w('#input1')) {


        var regExp = /.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/;

        var match = url.match(regExp);

        if (match && match[1].length === 11) {
            let urllink = match[1];
            let imagelink = "http:\/\/img.youtube.com\/vi\/" + urllink + "\/hqdefault.jpg\"";

            console.log(imagelink);
        } else {
            //Nothing
        }

    }

Thanks


Solution

  • According to wix documentation, $w function selects and returns elements from a page.

    If you have an input on the page with id set to input1 (you are trying to select it by calling $w('#input1')) than the variable url holds handle for the input, not the value of the input.

    So try to get the value from it (like url = url.value, or just url = $w('#input1').value), and after than call url.match(regExp);.

    Edit your function like this:

    function youtube(url = $w('#input1')) {
        var regExp = /.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/;
    
        url = url.value;
        var match = url.match(regExp);
    
        if (match && match[1].length === 11) {
            let urllink = match[1];
            let imagelink = "http:\/\/img.youtube.com\/vi\/" + urllink + "\/hqdefault.jpg\"";
            console.log(imagelink);
        } 
        else {
            //Nothing
        }
    }