user2251126
user2251126

Reputation: 25

Retrieving element with no name or id

I am trying to write some code to accomplish something very simple. I have never worked with a PayPal button before, which is causing some difficulty.

I have been able to simulate a button click before, using document.getElementById or document.getElementsByName(), however, the form I am trying to submit above does not have a name, or an id, so I do not know how to refer to it in my code.

I am trying to write a short chrome extension using Javascript that will find the paypal form/button on the page (that I do not own or have control over), and submit it without me having to click it. Any guidance/links would be much appreciated.

Upvotes: 1

Views: 1863

Answers (3)

Based on your update, first off: don't do this, unless you're trying to swindle people out of money. In which case, don't do this =) (I'm not a lawyer, but I'm pretty sure paypal can go "well you didn't click the button, you automated it. And now someone's site made you spend $500 that you didn't want to. tough"). That said: document.getElementsByTagName("form"), then filter on getAttribute("action").

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

Looks like your button has a name, so you can use .getElementsByName()

var btn = document.getElementsByname('submit')[0]

If you have access to jQuery or you can add jQuery to your project then

var btn = jQuery('input[name="submit"]')

Upvotes: 1

Brian Vanderbusch
Brian Vanderbusch

Reputation: 3339

This would be MUCH easier with jQuery, but with native js, you could do:

function getPaypalForms()
{
  var matchingElements = [];
  var allForms = document.getElementsByTagName('form');
  for (var i = 0; i < allForms.length; i++)
  {
    if (allForms[i].getAttribute('action') == 'https://www.paypal.com/cgi-bin/webscr')
    {
      // Element exists with attribute. Add to array.
      matchingElements.push(allElements[i]);
    }
  }
  return matchingElements;
}

There are many ways to edit this function above to suit your purposes even better. You could edit it to collect just the submit buttons from these forms, or refactor it to accept a string to match an attr, and value. However, like I said, this would be much easier using jquery:

$('form[action="https://www.paypal.com/cgi-bin/webscr"]');

Upvotes: 0

Related Questions