-1

I have the following scenario..

When the user visit my website I show the user some audio files from the server. When the user clicks on one link, then the jquery function is eventually executed and change the source of the embedded object to selected file.

The codes are given in this link. I got some answers for this question but the problem isn't solved.

What happening for me is, When I click on the link. The song plays in new page. I need to play the song with embedded object inside the div.

2 Answers 2

2

Try this:

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
  $(function(){
    $(document).on('click', "#song", function(e){
      e.preventDefault(); //<----------stops to navigate to link
      var musicSrc = $(this).attr('href');
      $('#music').find('embed').attr('src', musicSrc);
    });
  });
</script>

note:

make sure to load the jQuery library first:

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
Sign up to request clarification or add additional context in comments.

5 Comments

You will find the same answer in the link he provided.
No sir it is not working.When I click on the link, Song will play in new page(Refreshing the current page and changing the address to http://localhost:8080/skypark/sp/resources/songs/xxx22.mp3)
Yup. It would suck if he didn't have jQuery loaded.
@d4rkpr1nc3 yeah it seems that he is not referencing the library.
Thank you sir now it was playing. I combined both the answers in this question now it is working. Thank you very Much sir.....
1

Well of course it's not working. The event should be added after the page loads. Using jquery, it would look like this :

<script type="text/javascript">
$(function () {
    $(".song").click(function(e) {
        $('#mplayer').attr('src', $(this).attr('href'));
        e.preventDefault();
    });
});
</script>

6 Comments

No sir it is not working.When I click on the link, Song will play in new page(Refreshing the current page and changing the address to http://localhost:8080/skypark/sp/resources/songs/xxx22.mp3)
you also did the same thing.
No. The logical answer is to add the event on page load, otherwise the element would not exist.
@james, please post you code in the question or create a fiddle
Thank you sir now it was playing. I missed to add the function you specified before my function.... Thank you very Much sir.....
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.