Nitesh Bhandari
Nitesh Bhandari

Reputation: 13

To use console window in chrome to execute .click() command

I am trying to use chrome's console window to execute click() on the apply coupon button class to check whether it can be applied or not. I am not able to think of a way where I can send the input text when I type document.getElementsByClassName(clasName).click() in console button. It should take the input value and on clicking on apply button it should check on the backend and relevant message to be displayed (success/failure). Please help. Here's my code

document.getElementsByClassName("_381fS")[0].value = 123;
var y = document.getElementsByClassName("_3keqX")[0];
y.onclick = function() {
  $.ajax({
                url : "https://www.swiggy.com/dapi/cart/applyCoupon",
                data: document.getElementsByClassName("_381fS")[0].value,
                type: "POST",
                dataType: "text",
                contentType: false, 
                processData: false,
                success : 
                    function(response) {
                        alert("NO");
                    }
        });
};

Upvotes: 0

Views: 1166

Answers (1)

Rikin
Rikin

Reputation: 5473

First document.getElementsByClassName returns you HTML Collection i.e. an Array. Assuming you are going to use very first element you will have to use document.getElementsByClassName(name_of_class_in_string)[0] Now assuming your HTML element returned is a button or input then you can trigger .click() on it.

For more like example head over here: Trigger a button click with JavaScript on the Enter key in a text box

Upvotes: 1

Related Questions