SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    May 2009
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript problem with function

    Hi all,

    I have create function to retrieve the value from click event. But the variable still undefined.

    Code:

    <span class="dialink" onclick="get_song('s1')">Send</span>

    Code:

    <script type="text/javascript" language="javascript">

    var song_title;

    var title1='Test1';
    var title2='Test2';


    function get_song(tt){

    if(tt=='s1'){
    song_title=title1;
    }else if(tt=='s2'){
    song_title=title2;
    }

    return song_title;
    }

    </script>


    Then I have create another section to show the value of that variable but it shows undefined.

    Code:

    <script type="text/javascript" language="javascript">document.writeln(song_title);</script>

    What wrong with this?

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The second script (document.writeln()) is executed as the page loads, i.e., before the onclick event has occurred and assigned a value to your global variable.

    You need to display the new song title in the event handler.
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Member
    Join Date
    May 2009
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello.
    Thank you for reply.
    i will try your suggestion.


    When someone shares something of value with you and you benefit from it,
    you have a moral obligation to share it with others.

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Create a DIV or any html element then give a unique id. And show the content/title in that element as innerHTML.
    If you have div named 'showTitle' like this:
    HTML Code:
    <div id="showTitle"></div>
    And your function should look like this:
    Code javascript:
    function get_song(tt){
     
    if(tt=='s1'){
    song_title=title1;
    }else if(tt=='s2'){
    song_title=title2;
    }
    document.getElementById('showTitle').innerHTML = song_title;
    }
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •