JQuery .click() triggers after the second click only

I'm trying to use JQuery load() instead of javascript ajax. I have multiple of ids generated for each DIV with php loop.

When creating a JQuery function like below I pass the generated ids of divs fine but, I will have to click twice on the button in-order the .click() event to trigger , I put a window.alert() inside the .click() function and it popups twice after the second click on the button, any idea how to do it the correct way?

UPDATED

function updateDivs(generatedID){
    $('#'+generatedID).click(function(){
        //do some .load() stuff
    });
}

<?php
 for($i=0 ; $i < 20 ; $i++){
   $gen = mt_rand(100,999);
?>
   <div id="<?=$gen;?>" onclick="updateDivs(<?=$gen;?>) > some content </div
<?php
}
?>

Answers


I think that the easiest way is to set a class for your divs and inside you can get the div id with event.target.id.

I think that your problem is that you need to have something like document.ready() or $(function() { });

<div id="div_id" class="mydiv"> some content </div>
<script type="text/javascript">
           $(document).ready(function () {
        $(".mydiv").click(function (event) {
            alert(event.target.id);
        });
    });
</script>

I think what you want is binding a doubleclick event handler, which can be done this way:

$("<?=$gen;?>").on("dblclick", function (e) {
   // do some stuff here.
});

Need Your Help

Block Placement Algorithm

algorithm

I currently have a square, and a series of rectangles of the following sizes:

how to delete a specific row in the html table and database

javascript html jsp

I wrote a table in html which consists of 6 columns with 6th column as a delete button.The rows will gets updated dynamically.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.