1

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

8
  • which version of jquery are you using ? Commented Apr 19, 2013 at 11:33
  • $(".u_search").attr('id') will always return the ID of the first element with that class. Does $(this).attr('id') (i.e. your attr_u_search) not give you what you want? Commented Apr 19, 2013 at 11:36
  • jquery version 1.9.1 (provided by google) Commented Apr 19, 2013 at 11:36
  • Ok see my answer, I changed how you call the click() Commented Apr 19, 2013 at 11:42
  • 1
    Please post your HTML code too. THe problem doesn't seem to be with the jQuery code. Commented Apr 19, 2013 at 11:48

8 Answers 8

1

Inside your function change

$('.u_search').attr('id')

To

$(this).attr('id')

The quotes are important, but this is not inside quotes ;)

Sign up to request clarification or add additional context in comments.

Comments

0

When you do

$(".u_search").click(function() {...

your callback function has now an object called this. You can use it to get the actual object you've clicked. So in this case, if you want to get the ID of the clicked object, simply do this:

var the_id = $(this).attr("id");

3 Comments

seems that you have not read the question ... he is using var attr_u_search = $(this).attr('id');
You've edited your code. You've previously used the search() function!
You don't find it weird that three people answered the question as if he wasn't using this already? Also, if you make an edit quickly enough, the question won't indicate it.
0

Inside a function, you can reference to the active jquery object by using $(this)

$(".u_search").click(function() {
 ...
 //get ID of button being clicked upon
//Jquery Solution
 $(this).attr('id');

//Or Javascript
  this.id;
 ...
}

1 Comment

The question has been edited, that's why a few people have provided this answer.
0
  $("div").on("click",function(){
     var id = this.id; //returns id
   });

Comments

0
var attr_u_search = $(this).attr('id');
var dataString = '&u_search=' + $(".u_search").attr('id');

You're selecting every u_search div again to build your datastring, instead of using the already fetched id:

var attr_u_search = $(this).attr('id');
var dataString = '&u_search=' + attr_u_search;

Comments

0

i think it's : $(this).attr('id') You need use $(this) to have the object that clicked..

var attr_u_search = $(this).attr('id');
var dataString = '&u_search=' + attr_u_search; // it's better !

3 Comments

the read the question carefully before answering ... he already used your answer
On beginning, he had $('u_search') ...And when he try to get the good id, he don't use $(this) !
in every .click(...) change $('u_search') by $(this).
0

Try this :

$('body').on('click', '.u_search', function() {
  var attr_u_search = $(this).attr('id');
  var dataString = '&u_search=' + attr_u_search;
  alert(dataString);
  // REST OF CODE
});

(Tested and working, if it still does not work, the issue might be somewhere else)

1 Comment

check that you include JQuery before the code and that the code comes after the html
0
$('body').delegate('.u_search', 'click', function() {
    var attr_u_search = $(this).attr('id');
    var dataString = '&u_search=' + attr_u_search;
    alert(dataString);
});

2 Comments

Try to alert value of attr_u_search
the first codes dont work at all. the second one shows the id of the first div as alert...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.