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

admin2025-04-19  0

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

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

Share Improve this question edited Dec 3, 2013 at 6:47 Tushar Gupta - curioustushar 57.1k24 gold badges106 silver badges109 bronze badges asked Dec 3, 2013 at 6:47 user1314601user1314601 3
  • 2 getelementbyqueryall does not even exist ;) Anyways, only you know the plete HTML. You have to find selector which narrows down the selection as much as possible and then select the correct element, e.g. by index. E.g. if there is only one element that matches .call-to-action input[type=submit] then you already got it. If there are multiple and yours is the second one, get the second element from the set. – Felix Kling Commented Dec 3, 2013 at 6:48
  • Query by type then filter by value? – elclanrs Commented Dec 3, 2013 at 6:49
  • relevant code <div class="call-to-action call-to-action-g1"> <input type="submit" value="Siguiente paso" class="submit"> </div> – user1314601 Commented Dec 3, 2013 at 7:02
Add a ment  | 

9 Answers 9

Reset to default 3

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)

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;
}

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

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

Have you tried using class :

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

or

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

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();

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");
    }
});

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./category/selectors/

Try this pure-javascript solution:

document.querySelector(".call-to-action .submit").onclick = function() { 
  alert('bla bla'); 
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745002853a279336.html

最新回复(0)