I have the following HTML structure:
<div class='message_top' id='top_7' style='cursor:pointer;'>
<div class='top_ime'>From: official</div>
<div class='top_status' id='status_7'>
<img src='images/message_new.png' height='26' title='New message' />
</div>
</div>
In addition to this, there are a lot of other DIVs of the same structure, but with different ID numbers, for example:
<div class='message_top' id='top_15' style='cursor:pointer;'>
<div class='top_ime'>From: official</div>
<div class='top_status' id='status_15'>
<img src='images/message_new.png' height='26' title='New message' />
</div>
</div>
Now, I have a jQuery function which takes the ID of a .message_top
div and it should perform an action using the contents of the .top_status
div which belongs to it. However, for some reason, in the syntax it doesn't seem to recognize the ID of the .top_status
div. I know I'm nto explaining this very well, but here's a fiddle that will help your understand (code and commentary are there):
$(function () {
$('.message_top').click(function(){
var id = this.id;
status_id = id.replace("top","status");
status = $("#"+status_id).html();
alert(status_id+" "+status); //status variable is empty, but...
status2 = $("#status_7").html();
alert(status2); //status2 has the expected value
});
});
So, as you see, if I call the .html()
method using $("#status_7").html();
it works fine, but if I call it with $("#"+status_id).html();
(with status_id being "status_7") it doesn't work. What's happening here? I've used similar code a bunch of times and never had any issues.