Question

I have several a tags as shown below on a dynamically generated page

<a href='JavaScript:SWETargetGotoURL(vtUrl+"XRX+eCustomer+Account+Inventory+View", "_self")'  id='s_ViewBar' name=s_ViewBar>&nbsp;Inventory&nbsp;</a>

I need to traverse through all the a tags containing the text "SWETargetGotoURL" in their href tag and depending on the text I need to add the title tag

for example

if(extractedtext == "&nbsp;Inventory&nbsp;")
   title = "This is inventory homepage";

The a tag should finally look like this

<a href='JavaScript:SWETargetGotoURL(vtUrl+"XRX+eCustomer+Account+Inventory+View", "_self")'  title = "This is inventory homepage" id='s_ViewBar' name=s_ViewBar>&nbsp;Inventory&nbsp;</a>

I used following code to get all the tags with specific value in href tag but I am unable to get the text in between and I don't know how to run a loop and work on all the a tags

 var screenName = $('a[href*="SWETargetGotoURL"]').attr('href');

Any help is really appreciated.

Was it helpful?

Solution

I think what you're looking for is $.each which iterates over the set of selected elements.

$('a[href*="SWETargetGotoURL"]').each(function () {

    //get the text of the element
    var extractedtext = $(this).text();

    //check text for match
    if (extractedtext == "&nbsp;Inventory&nbsp;") {

        //Change title attribute of element
        $(this).attr('title', "This is inventory homepage");
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top