Reputation: 285
I have the following html code.
<div class="main_links cf" id="main_link">
<a class="est_btn" id="#electric">
<img src="images/electric.png" alt="" />
<span>Electric</span>
</a>
<a class="est_btn" id="#gas">
<img src="images/gas.png" alt="" />
<span>Gas</span>
</a>
<a class="est_btn" id="#oil">
<img src="images/oil.png" alt="" />
<span>Oil</span>
</a>
<a class="est_btn" id="#propane">
<img src="images/propane.png" alt="" />
<span>Propane</span>
</a>
</div>
Now i want atleast one of these anchor tags should be clicked. Can anyone tell me how it can be done with jquery ?
Upvotes: 1
Views: 11401
Reputation: 139
$( ".est_btn" ).click(function() {
console.log("clicked Anchor Id =" + $(this).attr('id'));
});
Upvotes: 0
Reputation: 388316
Use a class to store the clicked state
var $links = $('#main_link .est_btn').click(function () {
$(this).addClass('clicked');
});
//for test
$('button').click(function () {
if ($links.is('.clicked')) {
alert('clicked')
} else {
alert('not')
}
})
Demo: Fiddle
If you want to allow the user to select/unselect an item the use toggleClass()
instead of addClass()
Demo: Fiddle
Upvotes: 2
Reputation: 387
Try this,
$( ".est_btn" ).click(function() {
alert( "Clicked" );
});
Upvotes: 2