I’m still learning jQuery and I ran into a problem. I have a table with 50+ rows. Each is labeled with an id.
<tr id="51">
This id is the database id and I need to pass it to jQuery and submit it along with an AJAX request.
$($location).click(function () {
var $currentSelection = $(this);
// if there is already an input-field in this TD, return...
if($currentSelection.find('select').length > 0)
return;
$currentSelection.html('');
$('<select id="selectLocation" onChange="locationSubmit()"/>')
.attr('name', 'location')
.append('<option>Location 1</option>', '<option>Location 2</option>', '<option>Location 3</option>')
.appendTo(this);
});
locationSubmit()
function locationSubmit(){
var $selectLocation = $('#selectLocation').val();
var $selectId = $(this).val('tr#id'); //stuck here
alert($selectId);
$.ajax({
url: '/ajax/training-update.php',
data: {
action: $selectLocation,
courseid: $selectId
},
type: 'POST',
success: function(output) {
alert(output);
}
});
}
the alert in locationSubmit() is returning [object Object]. How do I pass the value of tr#id to locationSubmit() and send it via AJAX?
EDIT – HTML
<tr id="51">
<td><a href="/link/goes/here.php?course_id=5&id=51">course name</a></td>
<td>
<span class="rowStartDate" id="rowStartDate151">09/10/12</span> -
<span class="rowEndDate" id="rowEndDate151">09/14/12</span>
</td>
<td class="location">Location 2</td>
<td class="status">open</td>
</tr>
So, there is any good answer?
Test this:
and replace your function with this:
And as you see, your function is gone and replaced by the delegate, which seemed to work great! The .delegate() is a very good function that is working all the time, even if elements are created after the DOM is ready. It’s listening all the time
Edited
Check the second code part.