Search code examples
javascriptjqueryjplayer

How can I pass a variable in the path


I have this script and i want to play the song which has the name 'pr_id', how can i write it in the path? Thanks!

     $(document).ready(function(){
            var pr_id = document.getElementById('id')
            $("#jquery_jplayer_1").jPlayer({
                ready: function (){
                    $(this).jPlayer("setMedia", { 
                        mp3:"/sounds/[pr_id].mp3"
                        });
                        },
                       swfPath: "js", 
                       supplied: "mp3",
                       wmode: "window"
                    });
                });

Solution

  • Like so:

    $(document).ready( function() {
        var pr_id = document.getElementById('id');
    
        $("#jquery_jplayer_1").jPlayer({
            ready: function() {
                $(this).jPlayer("setMedia", { 
                    mp3:"/sounds/" + pr_id + ".mp3"
                });
            },
            swfPath: "js", 
            supplied: "mp3",
            wmode: "window"
        });
    });
    

    Concatenate the string with the variable :), unfortunately, unlike some other languages it's not as easy to include variables in string, and you have to split the string up a bit with the concatenation.