pravin kottawar
pravin kottawar

Reputation: 23

How can I use javascript to "click" a button that has no id or name?

here is relevant code

<div class="call-to-action call-to-action-g1">

<input type="submit" value="Siguiente paso" class="submit">

I can not modify this HTML code . So all I have is this only . The page also has many submit buttons so getelementbyqueryall won't work

Upvotes: 0

Views: 3790

Answers (9)

Zero Fiber
Zero Fiber

Reputation: 4465

Jquery Solution

$('input[value="Siguiente paso"]').trigger('click');

Javascript Solution

document.querySelectorAll('input[value="Siguiente paso"]').click();

For Older Browsers

function getInputsByValue(value)
{
    var allInputs = document.getElementsByTagName("input");
    var results = [];
    for(var x=0;x<allInputs.length;x++)
        if(allInputs[x].value == value)
            results.push(allInputs[x]);
    return results;
}

Upvotes: 2

Vikram Deshmukh
Vikram Deshmukh

Reputation: 15676

Try this pure-javascript solution:

document.querySelector(".call-to-action .submit").onclick = function() { 
  alert('bla bla'); 
}

Upvotes: 0

Chris
Chris

Reputation: 339

There are many selectors in jquery, not only by id or name. Like this:

$('.submit').click();

also you can see more about the jquery Selector:http://api.jquery.com/category/selectors/

Upvotes: 0

Vignesh Subramanian
Vignesh Subramanian

Reputation: 7289

Refer to this fiddle You can still capture an event using the type !!

$('input').on("click",function(){
// your code here
});

if you want to refine it even more then you can get the value of clicked element using this.value and have your code there

$('input').on("click",function(){
alert("Button clicked");
    if(this.value=="Siguiente paso"){
    alert("Siguiente paso clicked");
    }
});

Upvotes: 0

fiddle Demo

$('input.submit').filter(function () {
    return $.trim(this.value) == 'Siguiente paso';
}).click(function () {
    alert('hi');
});


to click

$('input.submit').filter(function () {
    return $.trim(this.value) == 'Siguiente paso';
}).click();

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388406

Why not use the container elements to reduce the scope of the search

$('.call-to-action-g1 input.submit')....

if you have multiple submit within the container then use the attribute selector along with value as shown by @Royi

Upvotes: 1

David
David

Reputation: 4873

The only way I know of, without knowing the index of the item, would be to use getElementsByClassName, and then loop through them all comparing the value. If you have two buttons with the same value, pretty sure you're out of options.

Upvotes: 1

Ankit Tyagi
Ankit Tyagi

Reputation: 2375

Have you tried using class :

$('.submit').click();

or

$('.submit').trigger( "click" );

Upvotes: 0

Royi Namir
Royi Namir

Reputation: 148624

You can do this :

$("div.call-to-action.call-to-action-g1 > input[type='submit'].submit[value='Siguiente paso']").on('click',function (){...});

(this is the specific I could find according to your code)

Upvotes: 3

Related Questions