Search code examples
jquerytexthrefsiebel

logic only on href having certain text - jQuery


I have several a tags on a dynamically generated web page and they are like this

<a href='JavaScript:SWESubmitForm(document.SWEForm9_0,s_12,"s_9_1_35_1","1-3ZR4-1743")'  tabindex=997  id='s_9_1_35_1'>Add</a>

<a href='JavaScript:SWESubmitForm(document.SWEForm9_0,s_13,"s_9_1_36_1","1-3ZR4-1743")'  tabindex=997  id='s_9_1_36_1'>Cancel</a>

several others like mentioned above. I need to execute some logic when link with text 'Add' is clicked. I know one of the way is to attach click event to a tags and then check for text.

$('a[href*="SWESubmitForm"]').click(function(){
if($(this).text() == "Add"){
//do something
}

but I am not sure if this is most efficient way to do this.

Can you guys please suggest the right way of doing this?


Solution

  • Your code works but you have problems with brackets.I tested on jsfiddle

    $('a[href*="SWESubmitForm"]').click(function(){
    if($(this).text() == "Add"){
    //do something
        alert('ok')}
    });​