0

I am using jquery-mobile package to develop web based mobile application. From a php script I read the button id's from MySQL and send id's to JavaScript method to create a button for it. However I want to add onclick listener also, ofcourse, but I could not manage it. Any help please.

<div data-role="content2" >
    <script type="text/javascript">
        function createButton(n) {
            $('[data-role="page"]').append('<button  data-theme= "b" id="btnInit' + n + '" >' + n + '</button>');// here I want to add onclick listener
        }

    <?php
        include './_fonksiyon.php';
        $query = ("
            SELECT
                tbl_ustmenu.Id AS id,
                tbl_ustmenu.AdiT AS adi
            FROM tbl_ustmenu
            WHERE tbl_ustmenu.Aktif=1
            AND   tbl_ustmenu.UstMenuId=0
            ORDER BY tbl_ustmenu.Sira ASC
                                    ");
        $result = mysql_query($query);
        $count = 0;
        while ($count < mysql_num_rows($result)) {
            $i = (mysql_result($result,$count, ($id)));
            $count++;
        ?>
            createButton(<?php echo ($i) ; ?>);

        <?php
        }
        ?>
    </script>
    </div>
1
  • 1
    Consider indenting things ... er, differently. Commented Jul 17, 2014 at 13:50

3 Answers 3

1

Replace your createButton with the snippet below, or create a separate function using the same snippet.

<script type="javascript">
    var button = document.createElement("INPUT");
    button.type = "button";
    button.name = "button";
    button.value = "Click me";
    button.id = "<?php echo $i ?>";
    button.onclick = function() {
        // ...
        // function body
    };
    // you may append it to any element you need
    document.body.appendChild(button);
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

I like how you just went native with it :)
@MoshMage Old school :)
0

Save the button to a variable first, it's more readable

var btn = $('<button  data-theme= "b" id="btnInit' + n + '" >' + n + '</button>');
btn.click(function() {
    alert('here we go');
});
$('[data-role="page"]').append(btn);

6 Comments

Thank you for your help. It is solved by this way. By the way, I want to ask one more thing. "adi" variable in the query returns a string, how can I pass it to myfunction, createButton? I both need to pass integer and a string(id,adi)
1. Extend your createButton-function: function createButton(n,adi) {} 2. Extend your php-echo: createButton(<?php echo($i).",".echo($adi); ?>); Not very familiar with php, maybe you need to reference $adi correctly
I know that, but javascript does not accept strings as integers. I know I have to remove the quotations in it, but I am not sure how to :) thanks anyway
Ah, my fault. Try this: createButton(<?php echo($i).", '".echo($adi)."'"; ?>);
Sorry, I'm not familiar with PHP. <?php echo($i.",'".$adi."'"); ?> Your JavaScript output (view source) should be createButton(1, 'adi-string');
|
0

add a class to your button and create a bind on click, like this:

$('[data-role="page"]').append('<button  class="mybuttonclass" data-theme= "b" id="btnInit' + n + '" >' + n + '</button>');



$(".mybuttonclass").on("click",function(e){
    //..
});

1 Comment

It stops to create buttons after first one is created. It is solved, thanks anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.