Braheen
Braheen

Reputation: 29

Finding the specific Tag a

My HTML is:

<a id="showSlotsByLocation_" href="#" style="color:blue;" onclick="confirmAppt('28/05/2013','364301');">14.00 - 14.15</a>
<a id="showSlotsByLocation_" href="#" style="color:blue;" onclick="confirmAppt('28/05/2013','364303');">14.15 - 14.30</a>

Id name are same on all links. This is the main difficulty.

I want to click second link my javascript code are configure web browser is

if (location.pathname == "/abc")
{
    //alert('location found') this is ok found;

    var el = document.getElementsByTagName("a");
    for (var i=0;i<el.length;i++)
    {
        if (el.id == 'showSlotsByLocation_' && el.innerText.isEqual('14.15 - 14.30') && el.outerHTML.contains("confirmAppt('28/05/2013'"))
        {
            alert('link found') \\this condition not match;
            el.onclick();
        }

    }
}

What do i do to match the condition?

Upvotes: 1

Views: 120

Answers (1)

Niccol&#242; Campolungo
Niccol&#242; Campolungo

Reputation: 12040

You can't have two element with the same ID, IDs are unique.

When you will have changed the IDs, you'll can access them simply using document.getElementById('idOfYourElement')

EDIT: First of all, you need to declare a "current" variable that takes the current element in the loop, you can't use el.id because el is a collection of HTMLElements! I'm sorry I didn't noticed it before. So you need this(define the variable inside the for loop, just before the if statement):

var current = el[i];

Now that you have defined it, change this whole line with the code below.

if (el.id == 'showSlotsByLocation_' && el.innerText.isEqual('14.15 - 14.30') && el.outerHTML.contains("confirmAppt('28/05/2013'"))

I think this is the code that stops you. There are no functions called isEqual and contains in JS.

if (current.id == 'showSlotsByLocation_' && current.textContent === '14.15 - 14.30' && current.outerHTML.indexOf("confirmAppt('28/05/2013'") !== -1)

One last thing: innerText isn't a valid cross browser property, use textContent instead.

MDN Reference

Updated JS code

if (location.pathname == "/abc")
{    
    var el = document.getElementsByTagName("a");
    for (var i=0;i<el.length;i++)
    {
        var current = el[i];
        if (current.id == 'showSlotsByLocation_' && current.textContent === '14.15 - 14.30')//I'm not sure about this one, in case you want it just remove the comment and the last parenthesis && current.outerHTML.indexOf("confirmAppt('28/05/2013'") !== -1)
        {
            alert('link found');
            current.click();
        }

    }
}

Upvotes: 3

Related Questions