0

I have an ASP.NET repeater element I'm using as a clickable menu. I'm using jQuery to set the img src to indicate which item is currently selected. I have a solution that works, but I'm wondering if there's a more elegant way to use jQuery... my solution kind of feels like a workaround.

My HTML:

<div class="c_menuDiv">
    <img src="images/check_white.jpg" id="img_check" class="cd_checkImage" width="20px" >
    <asp:Label ID="lblIssuedTo" runat="server" Text='<%#Bind("IssuedTo")%>'>
    </asp:Label>
</div>

And my jQuery, on document-ready:

$('.c_menuDiv').each(function() {
    issuedToBox = $('#txt_issuedTo').val();          <<== FROM A TEXTBOX
    issuedToMenu = $(this).children("span").html();
    if (issuedToBox == issuedToMenu ){
         $(this).find('img:first').attr('src','images/check_selected.jpg');
         }
     })

My question is, is there a more elegant way to use jQuery and avoid that js IF statement? I ask only because I'd like to sharpen my jQuery skills.

Thanks for any advice.

buckshot
  • 315
  • 4
  • 15
  • It would probably be more helpful to post the generated HTML, rather than your serverside code? – adeneo Oct 10 '16 at 17:56
  • 1
    Nothing which would beat `if` with speed or readability. – Teemu Oct 10 '16 at 18:01
  • in jquery If statement is fast. Don't worry Jquery is also very fast – LateshtClick.com Oct 10 '16 at 18:07
  • @Teemu is right that the `if` statement is speedy. I'd be careful with the `==` unless you want type-coercion. `==` would be slower than `===` as we can bypass the type-coercion with the `===` operator, but we have to be more specific or strict with the data we put in. [Here's a stackoverflow on it](http://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) – JeremyS Oct 10 '16 at 18:23
  • Thanks to all. Looks like my solution isn't too dumb, I think I'll keep it. – buckshot Oct 10 '16 at 18:29

1 Answers1

1

You could do the same thing without jQuery. Unless you have some other reason to use it...

<div class="c_menuDiv">
    <asp:Image ID="Image1" runat="server" ImageUrl="/images/check_white.jpg" />
</div>

And then on PostBack just change the ImageUrl:

Image1.ImageUrl = "/images/check_selected.jpg";
VDWWD
  • 35,079
  • 22
  • 62
  • 79