Search code examples
uibuttonjquery-mobilemouseclick-event

jquery mobile add click event on nested button


i have a nested buttons and im trying to add click events on it but it seems not to be working.

this is my object

<div id="ui-50">
<div class="ui-rpc" data-role="controlgroup" data-type="horizontal" >
    <input type="button" id="rpc-bor" value="<<" />
    <input type="button" id="rpc-back"  value="<" />
    <span style="margin:3px"></span>
    <input type="text" id="rpc-jump" value=""  />
    <input type="button" id="rpc-next" value=">" />
    <input type="button" id="rpc-eor" value=">>" />
    <span style="margin:20px"></span>
    <label id="rpc-current" >0</label>
    <label id="rpc-separator" >/</label>
    <label id="rpc-total" >0</label>
    <span style="margin:20px"></span>
    <label id="rpc-rpp" >0</label>
</div>
</div>

im trying to add click event on id="rpc-eor" button using

$("#ui-50 .ui-rpc #rpc-eor").click(function(){
    alert("yay!");
});

but the event wont fire. what is the matter? please help! thanks in advance


Solution

  • I am seeing the console.log output as expected in this demo I ran in Chrome, using jQuery.mobile 1.0b1

    <!DOCTYPE html> 
    <html> 
      <head> 
         <title>Page Title</title> 
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.min.css" />
        <script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            $("div:jqmData(role='page')").live('pageshow',function(){
                $("#ui-50 .ui-rpc #rpc-eor").click(function(){
                    console.log("yay!");
                });
            });
        });
        </script>
    </head> 
    <body>
    <div data-role="page" id="home">
        <div data-role="header">
           <h1>Header</h1>
        </div>
        <div data-role="content">
            <div id="ui-50">
                <div class="ui-rpc" data-role="controlgroup" data-type="horizontal" >
                    <input type="button" id="rpc-bor" value="<<" />
                    <input type="button" id="rpc-back"  value="<" />
                    <span style="margin:3px"></span>
                    <input type="text" id="rpc-jump" value=""  />
                    <input type="button" id="rpc-next" value=">" />
                    <input type="button" id="rpc-eor" value=">>" />
                    <span style="margin:20px"></span>
                    <label id="rpc-current" >0</label>
                    <label id="rpc-separator" >/</label>
                    <label id="rpc-total" >0</label>
                    <span style="margin:20px"></span>
                    <label id="rpc-rpp" >0</label>
                </div>
            </div>
        </div>
    </div>
    </body>
    </html>
    

    Note: there is no document.ready() for jQuery.mobile so if you have wrapped the jQuery in just document.ready() then it might be causing your problem. It is better to bind on $("div:jqmData(role='page')").live('pageshow',...); to guarantee that the page is ready. That said, in this simple case it still works without the .live bind.