Anna
Anna

Reputation: 203

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"
                    });
                });

Upvotes: 1

Views: 203

Answers (2)

dotmido
dotmido

Reputation: 1384

mp3:"/sounds/"+pr_id+".mp3"

you may concatenate it

Upvotes: 0

Seer
Seer

Reputation: 5237

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.

Upvotes: 3

Related Questions