How to call url in jquery from anchor tag?

255 views Asked by At

I'm working on a web application, Here look at below this jquery codes:

$(document).ready(function () {

    var myPlaylist = new jPlayerPlaylist({
        jPlayer: "#jplayer_N",
        cssSelectorAncestor: "#jp_container_N"
    }, [
      {
          title: "<Here is song title>",
          artist: "<here is artist>",
          mp3: "Here is song URL", //Here i want to call URL from anchor tag..
          poster: "images/m0.jpg"
      }
    ], {
        playlistOptions: {
            enableRemoveControls: true,
             ................
             ................

Now here is my html code, i want to play this music:

<a href="Music/Linkin_park.mp3">Play Linkin park</a>

<a href="Music/Linkin_park2.mp3">Play Linkin park2</a>

<a href="Music/Linkin_park3.mp3">Play Linkin park3</a>

What should i use in this situation?

3

There are 3 answers

0
Vatsal On

Please try this

$( "a" ).click(function( event ) {
event.preventDefault();
alert($(this).attr('href'));
});

Hope this be of some help

Happy Learning :)

2
Wang On

Did you try the following ?

<a id="lp1" href="Music/Linkin_park.mp3">Play Linkin park</a>
<a id="lp2" href="Music/Linkin_park2.mp3">Play Linkin park2</a>
<a id="lp3" href="Music/Linkin_park3.mp3">Play Linkin park3</a>

And then calling $("#lp1").click(); with a onClick() method for playing each song.


Or maybe you looked for something like this :

var songtoplay = $("#lp1").attr("href");

$(document).ready(function () {

var myPlaylist = new jPlayerPlaylist({
    jPlayer: "#jplayer_N",
    cssSelectorAncestor: "#jp_container_N"
}, [
  {
      title: "<Here is song title>",
      artist: "<here is artist>",
      mp3: songtoplay,
      poster: "images/m0.jpg"
  }
], {
    playlistOptions: {
        enableRemoveControls: true,
         ................
         ................
0
user3164891 On

You can get an url with the following code $("#identifier").attr("href");

Since I don't know the structure of your application I suggest you do a foreach through all the a elements