I'm a newbie to Javascript and jQuery and am puzzled by the following code and results.
Why is my golfer_name in scope, but my team_id not in scope? And how do I fix it? It appears to be the array, and the variables set equal to an array element that are not available in the call back.
jQuery code
function submit_pick( sender ) {
var names = sender.attr('name').split('-');
var team_id = names[0];
var tournament_id = names[1];
var pick_number = names[2];
var golfer_id = sender.val();
var golfer_name = sender.find("option:selected").text();
var senders_parent = sender.parent().get(0);
sender.remove();
$(senders_parent).html('submitting pick...');
jQuery("#testdiv").append("submit pick before ajax request: "+names+', '+team_id+', '+tournament_id+', '+pick_number+', '+golfer_id+', '+golfer_name+'<br />');
jQuery.post(fantasy_golf.ajaxurl,
{'action' : 'insert_pick',
'team_id' : team_id,
'tournament_id' : tournament_id,
'pick_number' : pick_number,
'golfer_id' : golfer_id
},
function(response) {
if (response) {
jQuery("#testdiv").append("submit pick after response: "+names+', '+team_id+', '+tournament_id+', '+pick_number+', '+golfer_id+', '+golfer_name+'<br />');
}
},
'text'
);
}
Results from the #testdiv
submit pick before ajax request: 10,2,1, 10, 2, 1, 22, Aaron Baddeley
submit pick after response: undefined, undefined, undefined, undefined, 22, Aaron Baddeley
I find it interesting that the commented out lines of code (which were not originally commented out) caused the above error even though they were after what I thought the problem line of code was.
I apologize for not having all extra lines of code commented out prior to my question, I was in the midst of moving some code around and new I still needed to look at the code below there, I just never figured it would cause a problem above that point.
Thanks all who helped.
function submit_pick( sender ) {
var names = sender.attr('name').split('-');
jQuery("#testdiv").append(sender.attr('name')+"<br />");
var team_id = names[0];
var tournament_id = names[1];
var pick_number = names[2];
var golfer_id = sender.val();
var golfer_name = sender.find("option:selected").text();
var senders_parent = sender.parent().get(0);
sender.remove();
$(senders_parent).html('submitting pick...');
jQuery("#testdiv").append("submit pick before ajax request: "+names+', '+team_id+', '+tournament_id+', '+pick_number+', '+golfer_id+', '+golfer_name+'<br />');
jQuery.post(fantasy_golf.ajaxurl,
{'action' : 'insert_pick',
'team_id' : team_id,
'tournament_id' : tournament_id,
'pick_number' : pick_number,
'golfer_id' : golfer_id
},
function(response) {
if (response) {
jQuery("#testdiv").append("submit pick after response: "+names+', '+team_id+', '+tournament_id+', '+pick_number+', '+golfer_id+', '+golfer_name+'<br />');
// var names = $this.attr('id').split('-');
// var pick_id = names[1];
// var team_id = names[2];
// var tournament_id = names[3];
// var pick_number = names[4];
}
},
'text'
);
}