Search code examples
javascriptprototypejs

How to add a new button beside existing one that does not have an ID


I'm trying to add a new button object beside an already existing one, but it has no ID so I cannot simple select that object. The only object with an ID is the iframe. The page looks like this:

<iframe id="gsft_main" />
  <html>
    <head>...</head>
    <body>
      <table>
      ...
      </table>
      <table>
        <table>
        ...
        </table>
      </table>
      <button onclick="someCodeHere('')"> Submit </button>
      <script>...</script>
      <script>...</script>
      <script>...</script>
    <body>
  </html>
</iframe>

I need to change it so it looks like:

...
...
<button onclick="injectedCodeHere('')"> Do Something Else </button>
<button onclick="someCodeHere('')"> Submit </button>
...
...

Thanks


Solution

  • Since this is the last button on the page, select all elements of type button and then take the last one from the array:

    var frame = ... get frame by id ...
    var buttons = frame.contentDocument.getElementsByTagName ('button');
    var lastButton = buttons[buttons.length-1];