I have been trying to get the id of a clicked div of same classes but with different IDs. I tried everything I know.
Here is one code I tried (in this one, the .click function() does not work .. edit: meaning it does not seem to run the code when clicked at all!):-
$(".u_search").click(function() {
var attr_u_search = $(this).attr('id');
var dataString = '&u_search=' + $(".u_search").attr('id');
alert(dataString);
$.ajax({
type: "POST",
url: '/script/profile.php',
data: dataString,
cache: false,
success: function(data) {
$('#ui_profile').show();
$('#ui_profile').html(data);
location.hash = 'profile' + 'id=' + dataString;
$(".searchbox").val('');
$("#usr_suggest").hide();
}
});
});
PHP:-
echo "<tr id='" . $id . "' class='u_search' height='40px'><td width='40px'><img class='avatar' src='$avater' /></td><td>" . $fname_ . " " . $lname_ . "</td></tr>";
}}
Here is another combination of codes I tried (error in this one: suppose I have 5
divs, and even if I clicked the 2nd div or the 3rd div, it only captures the id of the first div [div 1] and not the clicked div. I want to be able to capture the id of the clicked div.):-
$(".u_search").click(function() {
var attr_u_search = $(".u_search").attr('id');
var dataString = '&u_search=' + $(".u_search").attr('id');
alert(dataString);
$.ajax({
type: "POST",
url: '/script/profile.php',
data: dataString,
cache: false,
success: function(data) {
$('#ui_profile').show();
$('#ui_profile').html(data);
location.hash = 'profile' + 'id=' + dataString;
$(".searchbox").val('');
$("#usr_suggest").hide();
}
});
});
PHP:-
echo "<tr id='" . $id . "' class='u_search' height='40px' onclick='javascript:op_prof(1)'><td width='40px'><img class='avatar' src='$avater' /></td><td>" . $fname_ . " " . $lname_ . "</td></tr>";
}}
edit: when I use the first code (the one with .click function()) the code does not seem to run at all! .. I am using jquery library version 1.9.1
$(".u_search").attr('id')will always return the ID of the first element with that class. Does$(this).attr('id')(i.e. yourattr_u_search) not give you what you want?