KS1
KS1

Reputation: 1039

jQuery / Href parameter

I'm look for a way I can have a list of countries using ... so when you click on a link, the country code parameter is passed over to qjuery which will inturn return a list of cities.

I have been able to do all this using a dropdown, but want to change it to a.href links.

Something like the below...

$('#country').click(function(event) {  
// get the countryCode value here
var $country=$("a#country").val();
...

These need to pass countryCode

<a href="#" id="country">Australia</a>
<a href="#" id="country">United Kingdom</a>
<a href="#" id="country">NewZealand</a>

Upvotes: 1

Views: 380

Answers (4)

Mariusz Jamro
Mariusz Jamro

Reputation: 31692

In html5 its best to use custom attribute:

<a href="#" class="country" data-code="au">Australia</a>
<a href="#" class="country" data-code="uk">United Kingdom</a>
<a href="#" class="country" data-code="nz">NewZealand</a>

In JS:

$('.country').click(function(event) {  
    var $country = $(this).data('code');
}

Upvotes: 5

zkanoca
zkanoca

Reputation: 9928

<a href="#" class="c" id="country1">Australia</a>
<a href="#" class="c" id="country2">United Kingdom</a>
<a href="#"  class="c" id="country3">NewZealand</a>


$('.c').click(function(event) {  

event.prenventDefault(); 
var $country=$(this).html();
//var $country=$(this).text(); //or you can try this.
//...
});

Upvotes: -1

ArrayKnight
ArrayKnight

Reputation: 7356

First off, you shouldn't have multiple items with the same id.

You could store the country code as data:

<a href="#" class="country" data-country="aus">Australia</a>

And then retrieve it:

$('.country').on('click', function(e) {
    e.preventDefault();
    var country = $(this).data('country');
})

Upvotes: 1

tymeJV
tymeJV

Reputation: 104795

Change that id to a class and do:

$(".country").click(function(e) {
    e.preventDefault();
    var country = $(this).text();
});

Upvotes: 1

Related Questions