tags:

views:

51

answers:

5

I have a side-menu with labels under it define the menu-items and are dynamically generated. When user-clicks on any item, following code must execute:

$("#label").click( function(event) ){
    $.get(
        "../../phplibraries/productlist.php",
    {
        ControlName: 'ulCategoryName',
        Category: 'Beverages'
    },
    function(data) {
        $('#divList').html(data);
    });
});

I am not following how to capture click on any label that is actually a menu item.

-- Edited --

The HTML of side menu is as below:

<?php
    error_reporting(E_ALL^E_NOTICE);
    ini_set('display_errors', '1');

    require_once('dbaccess.php');

    $ulName = $_GET['ControlName'];
    $category = $_GET['Category'];

    $result=mysql_query("SELECT pname FROM products WHERE category='".$category."'");
?>
<ul id="<?php echo $ulName; ?>" name="<?php echo $ulName; ?>">
<?php while($row=mysql_fetch_array($result))
{ ?>
    <label id="lblCategory" name="lblCategory" style="font-size:medium;"><?php echo $row[1]; ?></label><br />
    <!--<li style="font-size:medium;"><?php echo $row[1]; ?></li>-->
<?php } ?>
</ul>
A: 

try removing the hash (#):

$("label").click(function(event)){
    $.get("../../phplibraries/productlist.php", {ControlName: 'ulCategoryName', Category: 'Beverages'}, 
    function(data){
        $('#divList').html(data);
    }); 
});

This will bind EVERY label. You may want to limit it. E.g.

$("#SomeContainerDiv label").click(function(event){
James Wiseman
+2  A: 

You have a syntax error in the first line:

$("#label").click( function(event) ){

should be:

$("#label").click( function(event) {

And remove the hash (#). #something means select element with ID 'something'. Since you want to bind this event handler to all your labels simply use $('label').

See jQuery's manaul on Selectors for more information on how to select various elements.

Jan Hančič
Thanks. Do see the edited version of my OP.
RPK
OP means Original Poster :) just so you know :)
Jan Hančič
I had a notion that it is Original Post.
RPK
Well could be :) Both makes sense, but here we refer to the first "post" as question, and everything else as an answer. I'm being nit-picky :)
Jan Hančič
A: 

Remove '#' from your code rest will fine

chirag
It is not working
RPK
if your label is generated dynamical then you have to use live jquery functionality, $('label').bind(function(){});
chirag
+1  A: 

if you need dynamically loaded labels to get the behavior you described consider using jQuery live().
in general, you could define your behavior to certain elements, with the needed selector, when creating the menu, and all dynamically created elements who answer to this selector will gain the same behavior.

kfiroo
Great. It worked.
RPK
A: 

since you are dynamically generating, I would look into using the .live('click'...) functionality for this one.

Mark Schultheiss