Tsukimoto Mitsumasa
Tsukimoto Mitsumasa

Reputation: 582

Passing a value in my click() function

I have this piece of code,

<a href="#" id="addtoteam">
    Add to Team
</a>

, and if I click this link, a modal dialog will appear,

$('#addtoteam').click(function(){
      var url = '<?php echo BASEURL;?>';
      $( "#dialogbox" ).dialog({ 
        autoOpen: true,
        buttons: { 
                  "Use Registration Credits": function(){ 
                    // execute something here before redirecting it to another page, perhaps perform the updating of the registration_credits of each team here
                    window.location = url + '/administration/add_to_team/' + teamID +'/'+ playerID +'/?credit=1'; 
                  },
                  "Pay Extra Charge": function() { $(this).dialog("close"); },
                  "Cancel": function() { $(this).dialog("close"); } 
                },
        width: 800
      });
    });

As you can see I have three buttons, take a look at the "Use Registration Credits" button, if it is chosen, it will redirect to another page, but what I want is if it redirects to other page, I want to have the url looking like this,

sample.com/administration/add_to_team/team_id/player_id?credit=1

Now my question is, is it possible to pass any value that can be assigned to a variable inside my click() function such that this value can be my team_id or player id? I hope I explained clearly. Thanks.

Upvotes: 0

Views: 112

Answers (3)

David Barker
David Barker

Reputation: 14620

You can use selectors to grab data from anywhere on the page, also if you specifically wanted to take data from the 'clicked' link you can use this in context of the click event.

$('#addToTeam').on('click', function()
{
    var clicked = $(this);

    // Get the clicked links HREF value
    var href = clicked.attr('href');

    // Get the clicked links NAME value
    var name = clicked.attr('name');

    // And so on
    var data = clicked.attr('data-value')

});

To fully answer your question, you would want to add some more data to your <a> tags. For simple stuff such as ID's etc, I just put them in the HREF value, and then use the technique above to pull it for the clicked link. You can then append that value to a query string.

$('#addtoteam').click(function()
{
    id = $(this).attr('href');

    window.location = url + '/administration/add_to_team/' + id

});

As an example

Upvotes: 0

adeneo
adeneo

Reputation: 318352

If you are trying to change the URL from:

sample.com/administration/add_to_team/team_id/player_id

to :

sample.com/administration/add_to_team/team_id/player_id?credit=1

All you're missing is the querystring, and you'll just add that like so :

document.location.search = '?credit=1';

Upvotes: 0

fliim
fliim

Reputation: 2199

you can store parameters like team_id or player id in your element as attributes:

<a href="#" id="addtoteam" data-team-id="4" data-player-id="5" >
    Add to Team
</a>

then you get these with:

team_id = $(this).attr('data-team-id');

Hope It can help you

Upvotes: 2

Related Questions