SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Wizard subnet_rx's Avatar
    Join Date
    Aug 2001
    Location
    Hattiesburg, MS
    Posts
    1,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript/jQuery newbie - need to pass variables to a function

    I have this button:

    PHP Code:
    <a id="m_location" href="#" data-role="button" data-icon="search" data-latitude="<?php echo $mLatitude?>" data-longitude="<?php echo $mLongitude?>">Help me find this</a>
    And I need to pass the data-longitude and data-latitude variables to a javascript function:

    Code:
    						$('#m_location').click(function() {
    							var mlatlng = new google.maps.LatLng($('#m_location').data('latitude'), $('#m_location').data('longitude'));
    							$('#map_canvas').gmap('search', { 'location': mlatlng }, function(results, status) {
    								if ( status === 'OK' ) {
    									$('#to').val(results[0].formatted_address);
    								}
    							});
    						});
    The above code doesn't work though, and nothing happens in the form field (#to) when I click. Help?

    Btw, I don't have to do this through data-attributes, I simply need to add the variables to the button, and then pass them to the function and run the function when the button is clicked. The page may contain many different buttons with different values that need to be passed on click.

  2. #2
    SitePoint Member Serj's Avatar
    Join Date
    Nov 2011
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try using attr():
    Code:
    var mlatlng = new google.maps.LatLng($('#m_location').attr('data-latitude'), $('#m_location').attr('data-longitude'));

  3. #3
    SitePoint Wizard subnet_rx's Avatar
    Join Date
    Aug 2001
    Location
    Hattiesburg, MS
    Posts
    1,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that didn't work either. Basically, nothing is happening when I click the link.


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
  •