SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Label value to javascript variable

    Hey,

    i'd like to have 2 javascript variables that will get its values from 2 labels. The problem is that the values are numbers and i get an error using my code. Any ideas on how this should be done?

    Code:
    function load() {
                if (GBrowserIsCompatible()) {
                    var map = new GMap2(document.getElementById("map"));
                    var lat = parseFloat(document.getElementById("lat_lbl").value);
                    var lng = parseFloat(document.getElementById("lng_lbl").value);    
                    map.setCenter(new GLatLng(lat, lng), 10);
                    map.addOverlay(new GMarker(new GLatLng(lat, lng)));
                                             }
                                 }

    Code:
    <asp:Label ID="lat_lbl" runat="server" Text="-33.9417"></asp:Label>&nbsp;
    <asp:Label ID="lng_lbl" runat="server" Text="150.9483"></asp:Label>

  2. #2
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What happens if you alert(document.getElementById("lat_lbl")) ?
    ..and what happens if you alert(document.getElementById("lat_lbl").value); ?
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  3. #3
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    alert(document.getElementById("lat_lbl"));
    returns "[object HTMLSpanElement]"

    alert(document.getElementById("lat_lbl").value);
    returns "undefined"

  4. #4
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What kind of element does the following return in HTML?
    <asp:Label ID="lng_lbl" runat="server" Text="150.9483"></asp:Label>

    If you inspect your html code - or do a view source even - what element is the Label as?

    You could try one of the following:
    alert(document.getElementById("lat_lbl").innerHTML);
    alert(document.getElementById("lat_lbl").Text);
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  5. #5
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What kind of element does the following return in HTML?
    not quite sure what you mean but this is what i get in source view "<span id="lng_lbl">150.9473</span>"

    alert(document.getElementById("lat_lbl").innerHTML);
    returns "-33.9417"

    alert(document.getElementById("lat_lbl").Text);
    returns "undefined"

  6. #6
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So your code would be:

    function load() {
    if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    var lat = parseFloat(document.getElementById("lat_lbl").innerHTML);
    var lng = parseFloat(document.getElementById("lng_lbl").innerHTML);
    map.setCenter(new GLatLng(lat, lng), 10);
    map.addOverlay(new GMarker(new GLatLng(lat, lng)));
    }
    }
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  7. #7
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sweet!! Thanks a million, it works!!


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
  •