1

I working with an existing website, with some anchors tags like so:

<ul>
    <li><a id="answer-1" href="/" rel="0">Answer 1</a></li>
    <li><a id="answer-2" href="/" rel="1">Answer 2</a></li>
    <li><a id="answer-3" href="/" rel="2">Answer 3</a></li>
    <li><a id="answer-4" href="/" rel="3">Answer 4</a></li>
</ul>

I need to programmatically click one of these anchors depending on the user's selection.

On the desktop, in IE, this works:

document.getElementById('answer-1').click();

However, doesn't quite work in iOS safari, this results in a popup:

if (!document.getElementById('answer-1').click) alert ('Oh no!');

Best I can tell, they have some other javascript hooked up using to send the rel tag as an answer. Is there another way to programmatically click the anchor in iOS Safari? Or do I need to go digging in their javascript to find out what is being called when the anchor is clicked?

I have also tried this, which doesn't work:

window.location.href = document.getElementById('answer-1').href;

They must be doing more in Javascript somewhere (maybe jquery).

jonathanpeppers
  • 26,115
  • 21
  • 99
  • 182
  • The `window.location` is an object. You should set it's `href`-property like this: `window.location.href = document.getElementById('answer-1').href` – Teemu Jan 29 '12 at 15:05
  • Sorry, I was typing that off the top of my head. I have adjusted my answer, it does not work. – jonathanpeppers Jan 29 '12 at 15:07
  • This is a safety issue. You can't examine local folders in all browsers. – Teemu Jan 29 '12 at 15:17
  • What does this have to do with folders? – jonathanpeppers Jan 29 '12 at 15:17
  • `Anchor` opens a folder. – Teemu Jan 29 '12 at 15:24
  • @Teemu That's incorrect. It will redirect to the root of the current website, not open a folder. – sciritai Jan 29 '12 at 17:18
  • @sciritai Original question includes this: "On the desktop, in IE, this works:". On a server, just like you said. This also seems odd to me: `if (!document.getElementById('answer-1').click)`. Expression refers to DOM's `click`-method. Events can't be handled thorugh it? – Teemu Jan 29 '12 at 17:37
  • To clarify: calling `click()` worked fine for me in IE9 on Windows 7. `if (myControl.click)` also evaluates to true in that browser. – jonathanpeppers Jan 30 '12 at 12:43

4 Answers4

3

I've answered to a similar question where one needs to dispatch click event manually: Force link to open in mobile safari from a web app with javascript

You can apply that logic into your code.

Community
  • 1
  • 1
Samuli Hakoniemi
  • 18,740
  • 1
  • 61
  • 74
0

Looks like the click function is not supported by Mobile Safari. document.getElementById('answer-1').click returns undefined which is why you get the alert. Try using .trigger()

sciritai
  • 3,688
  • 1
  • 17
  • 22
0

It looks to me like there is not a way to call click() in mobile safari.

Using the dev-toolbar in Google Chrome, I was able to figure out the javascript function that is invoked when you click one of these anchors.

I was able to just call the function to get what I need.

I will wait a few days to mark the answer, as I would still like to know an alternative to click() in mobile safari.

jonathanpeppers
  • 26,115
  • 21
  • 99
  • 182
-1

This problem occurred too with my Mac app when tested under Snow Leopard or Lion. Strangely it worked with Mountain Lion.

I solved the problem using jQuery.

$('#answer-1').click();

You may also give it a try if the site already loads jQuery!

zavié
  • 4,301
  • 2
  • 34
  • 46