find id of anchor inside gridview and set image

Posted by Klbaiju under jQuery on 3/4/2013 | Points: 10 | Views : 2674 | Status : [Member] | Replies : 1
HI all,
I have a gridview like this.anchor inside gridview contains an id
<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server"
Width="111px">

<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<a id='<%#Eval("pid")%>' href="#" ><image id ="img1" > </image> </a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


my requirement is i have a folder named dloadid,that contains some id's.
if the anchor id is matched with id in folder,set the src of image.
i tried this

$(document).ready(function () {

$("#<%=GridView1.ClientID%> tr:has(td)").each(function () {
var cell = $(this).find("td:eq(0)");
// var cell = $(this).attr("id");
// alert(cell.attr("id"));
var id = $('a').attr("id");
alert(id);
if(id=='./dload')
{
var nimage = './Images/pdf_icon.png';
$("#img1").attr('src', nimage);
}


});


});
but not working. any idea about this.
Regards
Baiju




Responses

Posted by: Devi0074u on: 3/12/2013 [Member] Starter | Points: 25

Up
0
Down
1. Remove the id img1 of the image.
2. replace var id = $('a').attr("id"); with cell.find('a') .
Check the following code (here a[id^=dload] means anchor whose id starts with dload, you can also use = or $= or *= as per your requirement) .

$(function () {
$("#<%=GridView1.ClientID%> tr:has(td)").each(function () {
var cell = $(this).find("td:first");
if(cell.find('a[id^=dload]').length > 0) {
$(this).find('img').attr('src', './Images/pdf_icon.png');
}
});
});

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response