0
<span id="continue" class="a-button a-button-span12 a-button-primary"><span class="a-button-inner"><input id="continue" tabindex="5" class="a-button-input" type="submit" aria-labelledby="continue-announce"><span id="continue-announce" class="a-button-text" aria-hidden="true">
          Continue
        </span></span></span>

Above the the HTML from part of a page, which has a 'Continue' button that i'm trying to click, using my script.

So, writing in Javascript, i'm trying to click this button. But nothing I have tried works.

My attempted answer is:

function() {


      var goButton = document.getElementById("continue");
     goButton.click();},

Why doesn't it work? Help me, please !

Hiya
  • 175
  • 1
  • 2
  • 9

1 Answers1

1

You have set the ID of both the span and the input field to "continue". ID's should be unique for a single element. When I enter your code in the browser's console it returns the following:

> var goButton = document.getElementById("continue");
< undefined
> goButton.valueOf()
< <span id="continue" class="a-button a-button-span12 a-button-primary">

You can see the span is the element being selected instead of the input submit button. You should rename either of the 2 elements so both have a unique ID and use that in your script.

Edit: OP mentioned the HTML can not be changed so instead of fixing the use of a not-unique ID this Javascript can be used:

function() { 
  var continueSpan = document.getElementById("continue"); 
  var goButton = continueSpan.firstElementChild.firstElementChild; 
  goButton.click();}
Tweek
  • 46
  • 6
  • 1
    I can't alter the HTML on the page - that is fixed. – Hiya Nov 20 '17 at 16:14
  • It won't get any prettier, but you can select the span and navigate to the input field by using selectChildElement function. `var goButton = document.getElementById("continue"); goButton.firstElementChild.firstElementChild.valueOf()` Output will be: `` – Tweek Nov 20 '17 at 16:21
  • I can't quite work out where the line breaks go. Do I still put `goButton.click();},` at the end? – Hiya Nov 20 '17 at 16:27
  • 1
    The valueOf line was just to display the result of what you were trying to do. So the actual script you need is this: `function() { var continueSpan = document.getElementById("continue"); var goButton = continueSpan.firstElementChild.firstElementChild; goButton.click();},` – Tweek Nov 21 '17 at 09:12