user2826169
user2826169

Reputation: 285

how to check if an anchor tag is clicked or not in jquery?

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

Answers (4)

Rakesh
Rakesh

Reputation: 139

$( ".est_btn" ).click(function() {
    console.log("clicked Anchor Id =" + $(this).attr('id'));
});   

Upvotes: 0

timo
timo

Reputation: 2169

$(".est_btn").click(function () {
   //your code here
});

Upvotes: 0

Arun P Johny
Arun P Johny

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

addy2601
addy2601

Reputation: 387

Try this,

$( ".est_btn" ).click(function() {
    alert( "Clicked" );
});    

Upvotes: 2

Related Questions