AJSwift
AJSwift

Reputation: 719

add querystring to URL

var EventAddress=$(".EventAddress").text();
$(".EventDirectionLink a").attr("href", url + EventAddress);  

This is the jquery I am using to get the address value from the table cell and pass it to the map url as a query string but its not working? What am I doing wrong??

<div class="driving-directions-link">
<a href="http://maps.google.com/maps?q=">Get Direction</a>
</div>

<table cellpadding="10" class ="EventDetail">
    <tr>
        <td class="TableFields">Who Should Enroll?:</td>
        <td>Everyone 18 and older who would like to attend</td>
    </tr>
    <tr>
        <td class="TableFields">Location:</td>
        <td class="EventAddress">1300 Albany Street,Beech Grove ,IN</td>
    </tr>
</table>

Upvotes: 2

Views: 5998

Answers (5)

David Thomas
David Thomas

Reputation: 253308

Try this:

$('td.EventAddress').click(
    function(){
        $(".driving-directions-link a").attr("href", $(this).attr('href') + encodeURIComponent($(this).text)); 
    });

JS Fiddle demo.

Simplified the above a little, to avoid using the .attr() method twice (the second time unnecessarily):

$('td.EventAddress').click(function(){
    $(".driving-directions-link a").attr("href", function(i, h) {
        return h + encodeURIComponent($(this).text());
    });

JS Fiddle demo.

References:

Upvotes: 4

Dan Bamber
Dan Bamber

Reputation: 41

I had a similar issue trying to add a querystring using:

$(this).attr("href", url + queryString);

It was adding the url but not the queryString.

I used the javascript concat method instead and this seemed to work:

var queryString = '?myVar=test';
//Replace the url with an additional query string
$(this).attr('href', $(this).attr('href').concat(queryString));

Upvotes: 0

Chibuzo
Chibuzo

Reputation: 6117

var EventAddress = encodeURIComponent($(".EventAddress").text());
$(".EventDirectionLink a").attr("href", url + "q=" + EventAddress);  

Remember to decode the querystring before you use it.

Upvotes: 0

balint
balint

Reputation: 3431

$(".driving-directions-link > a").attr("href", "http://maps.google.com/maps?q=" + EventAddress);

Upvotes: 0

Eric Pigeon
Eric Pigeon

Reputation: 1131

<div class="EventDirectionsLink">
<a href="http://maps.google.com/maps?q=">Get Direction</a>
</div>

<table cellpadding="10" class ="EventDetail">
    <tr>
        <td class="TableFields">Who Should Enroll?:</td>
        <td>Everyone 18 and older who would like to attend</td>
    </tr>
    <tr>
        <td class="TableFields">Location:</td>
        <td class="EventAddress">1300 Albany Street,Beech Grove ,IN</td>
    </tr>
</table>
var EventAddress=$(".EventAddress").text();
$(".EventDirectionsLink > a").attr("href", $('.EventDirectionsLink > a).attr('href') + EventAddress);  

Upvotes: 0

Related Questions