target a link with jquery

0 jamie y · September 26, 2015
I am trying to add a background image to a link using jquery so that when the link is clicked, it will change the image. I am using bootstrap accordion and need to add a sign on the link.

<div class="panel panel-default" id="panel1">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-target="#collapseOne"
href="#collapseOne">




I want to add it to the h4 with the class of panel. When the link is clicked, the accordion adds


aria-expanded="true"

I had hoped to target that so that when it changes from true to false, I can change the background image on click. Right now, I just tried to add the image and it isn't working. Can you check my code?



$('h4.panel-title a').css({"background-image": "url(images/panel-button.png)", "width": "19px", "height": "18px"}); 





Post a Reply

Replies

Oldest  Newest  Rating
0 Aakash Ahmed · September 29, 2015
You should write this:

var link = $("h4.panel-title").children("a");

link.on("click",function(){
var aria = $(this).attr("aria-expanded");
if( aria == false ){
$('h4.panel-title a').css({"background-image": "url(images/panel-button.png)", "width": "19px","height": "18px"});
}
});


and if problem occurs again in changing image, write only "background" instead of "background-image".
Hope this will help you!
  • 1

Javascript

107,325 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators