#1
  1. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    108
    Rep Power
    4

    Passing data from a jquery .click


    I have been trying to pass data from a jquery .click to the event
    handling function [implemented as an alert() stub below] by using
    the name attribute to contain the data. As you can see I am building
    the list of items that contain the buttons dynamically. The three
    screen shot files show the situation at a breakpoint just before
    the alert call.

    The game variable [set in line 21] is undefined even though the
    name attribute is correct. What am I doing wrong?

    javascript Code:
     
    function listReturn(response) {
      if (response.indexOf("<!doctype html>") !== -1) { // User has timed out.
        window.location = "access-denied.html";
      }
      var resp = jQuery.parseJSON(response);
      if (resp.stat === 'success') {
        var gameHTML ='';
        $.each(resp.games,function(index,listInfo) {
          gameHTML += '<tr class="gamerow"> <td>';
          gameHTML += '<a href="board18Map.php?dogame=';
          gameHTML += listInfo.game_id + ' ">';
          gameHTML += listInfo.gname + '</a></td> <td>';
          gameHTML += listInfo.bname + '</td> <td>';
          gameHTML += listInfo.version + '</td> <td>';
          gameHTML += listInfo.start_date + '</td> <td>';
          gameHTML += '<button type="button" name="';
          gameHTML += listInfo.game_id + '">Edit</button> </td> </tr>';
        }); // end of each
        $('#gamelist').append(gameHTML);
        $('.gamerow button').click({game: $(this).attr(name)}, function(e) {
          var game = e.data.game;
          var msg1 = 'button pressed - ' + game;
          alert(msg1);
        });
      } else if (resp.stat === 'none') {
        var nogames = '<p id="gamehead">';
        nogames += 'You are not currently playing any games</p>';
        $('#games').append(nogames);
      } else if (resp.stat === 'fail') {
        var errmsg1 = 'Program error in myGameList.php.\n';
        errmsg1 += 'Please contact the BOARD18 webmaster.';
        alert(errmsg1);
      } else {  // Something is definitly wrong in the code.
        var nerrmsg = 'Invalid return code from myGameList.php.\n';
        nerrmsg += response + '\nPlease contact the BOARD18 webmaster.';
        alert(nerrmsg);
      }
    } // end of listReturn
    Attached Images
  2. #2
  3. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    108
    Rep Power
    4
    I sidestepped this issue by changing my design to use a different page to display the new form. This allowed me to change the click to a link.

    The code in question changed to:

    Code:
        $.each(resp.games,function(index,listInfo) {
          gameHTML += '<tr class="gamerow"> <td>';
          gameHTML += '<a href="board18Map.php?dogame=';
          gameHTML += listInfo.game_id + ' ">';
          gameHTML += listInfo.gname + '</a></td> <td>';
          gameHTML += listInfo.bname + '</td> <td>';
          gameHTML += listInfo.version + '</td> <td>';
          gameHTML += listInfo.start_date + '</td> <td>';
          gameHTML += '<a href="board18Admin.php?dogame=';
          gameHTML += listInfo.game_id + '"><button type="button">';
          gameHTML += 'Edit</button></a> </td> </tr>';
        }); // end of each

IMN logo majestic logo threadwatch logo seochat tools logo