I’m wondering how to make an If Statement in a jquery script that checks if a source image file is a certain one, and if so execute code within If Statement. I want If Statement to execute its code only if source =
<%= Page.ResolveUrl("~/{0}/_res/_images/icon_noticeActive.png", PBS.Cms.Settings.PBSFolderName) %>". Here's my html code and jquery script below.
$("img#importantImg").click(function () {
if ($("#headerAlertMessage").is("<%= Page.ResolveUrl("~/{0}/_res/_images/icon_noticeActive.png", PBS.Cms.Settings.PBSFolderName) %>")
$("#headerAlertMessage").show();
$("#importantImg").attr("src", "<%=Page.ResolveUrl("~/{0}/_res/_images/icon_noticeActive.png", PBS.Cms.Settings.PBSFolderName) %>");
});
Here’s my html code that this script above references:
<li class="levelOne" ><a class="button notice buttonEight" href="#">
<asp:image id="importantImg" runat="server" ClientIDMode="Static" /></a></li>
</ul>
<asp:PlaceHolder ID="contentPlaceholder1" runat="server" EnableViewState="false" />
</div>
<div id="headerAlertMessage" runat="server" ClientIDMode="Static" style="display:none">
<div id = "divLayer1" class="alertPod">
<img src="<%= Page.ResolveUrl("~/{0}/_res/_images/icon_alertMessage.png", PBS.Cms.Settings.PBSFolderName) %>" />
</div>
<div id = "divLayer2" class="msgPod">
<div id="divLayer3" class="messageWrapper">
<h6>IMPORTANT ANNOUNCEMENT</h6>
<div id="divLayer4" class="box" >
<div id = "divLayer5" class="viewport" style="overflow: auto; height: 48px;" runat="server">
<p>
<asp:Literal ID="ltimportantannouncementTitle" runat="server"></asp:Literal>
<br />
<asp:Literal ID="ltimportantannouncementSummary" runat="server"> </asp:Literal>
</p>
</div>
</div>
</div>
<a href="#" ><img id="alertCloseBtn" ClientIDMode="Static" src="<%= Page.ResolveUrl("~/{0}/_res/_images/button_alertMsgClose.png", PBS.Cms.Settings.PBSFolderName) %>" /></a>
</div>
</div>
This will find all images with a specific source, iterate through them, which allows you to do whatever you want inside the
.each()function. Note thatvalueis the variable that holds the reference to the image within the.each()function.Here is a jsfiddle of selecting images by their source: http://jsfiddle.net/jasper/u5xWP/