Midhun Sivaraj
Midhun Sivaraj

Reputation: 503

Programmatically click a button on the html

I have an html page with jQuery for search.

!DOCTYPE html>
<html>
<head>
    <style>
    </style>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
    <div>
        <form id="searchForm">
            <input type="text" id="search" />
            <input type="submit" name="Search!" id="Search!"/>
        </form>
        <div id="resultContainer"></div>
    </div>

<script>
        jQuery(function(){
            $("#searchForm").submit(function (e) {
                e.preventDefault();
                var results = $("#resultContainer");
                var text = $("#search").val();
                results.empty();
                $.get("2.html", function (data) {
                    var els = $(data).find("div:contains(" + text + ")").appendTo(results);
                    els.find('a').contents().unwrap();
                    els.wrap(function(){
                        return $('<a/>', {
                            href: '2.html#' +this.id
                        })
                    })
                });
            });
        })
    </script>

</body>
</html>

I need to click the form button. I managed to put some values on the form using java script. My java code is

wb.loadUrl("javascript:(function({document.getElementById('search').value='"+myIds2+"';})()");
   Handler handler = new Handler(); 
   handler.postDelayed(new Runnable() { 
   public void run() { 
     wb.loadUrl("javascript:document.getElementById('Search!').submit();");
                                 } 
                            }, 2000);

I already tried these, but not working

wb.loadUrl("javascript:(function(){document.getElementById('searchForm').trigger('click'); })");
wb.loadUrl("javascript:document.getElementById('searchForm').submit();");

Upvotes: 0

Views: 861

Answers (2)

Imran Athar
Imran Athar

Reputation: 467

Can use any of bellow..

$('input[type="submit"]').click();

Or

$('#Search!').click();

you can call click() on any of the HTML control by ID of it.

$('#CONTRLID').click();

Hope it works for you..

Upvotes: 1

Mark
Mark

Reputation: 3123

This should trigger the click you are after:

$('input[type="submit"]').click();

Upvotes: 2

Related Questions