2

BIG EDIT 11/19/2012

So, my problem is here. I'm trying to develop a simple cart app with jQuery and XML.

Everything's OK, but only if the name of My pizz only contains numbers... WHY ???

Try yourself, you'll constat that only the "66666" pizza is working... WHY ???

WORKING DEMO HERE

Here is my jS code:

$(document).ready(function(){
 $.ajax({
  type: "GET",
  url: "example.xml", 
  dataType: ($.browser.msie) ? "text" : "xml",
  success: function(xml) {  
   $(xml).find('row').each(function(){
    var Col0 = $(this).find('nompizz').text();
    var Col1 = $(this).find('petit').text();
    var Col2 = $(this).find('moyen').text();
    var Col3 = $(this).find('grand').text();

    $('<tr id="bar"></tr>').html('<th class="title">'+Col0+'</th><td onclick="DisPlay('+Col1+');GetName('+Col0+');">'+Col1+'€</td><td onclick="DisPlay('+Col2+');GetName('+Col0+');">'+Col2+'€</td><td onclick="DisPlay('+Col3+');GetName('+Col0+');">'+Col3+'€</td>').appendTo('#pizzas');

   });
  }
 }); 
});
function DisPlay(Figure) {   
     var Display = document.getElementById('Display');
     if (Figure === null) {
         Display.value = Figure;
     }
     else {
         Display.value += "+"+Figure;
         Screen = Display.value
         result = eval(Screen);

         Display.value =result;
     }

      Figure = null;
}
function GetName(NomPizz) {
      var $newItem = '<li>Ajouté: '+NomPizz+'</li>';
      $('.theList').append($newItem); 
}

And here my XML:

<?xml version="1.0"?>
<document>
 <row>
  <nompizz>66666</nompizz>
  <petit>10</petit >
  <moyen>15</moyen >
  <grand>20</grand>
 </row>
 <row>
  <nompizz>Letters</nompizz >
  <petit>15</petit >
  <moyen>20</moyen >
  <grand>25</grand>
 </row>
</document>
Naftali
  • 144,921
  • 39
  • 244
  • 303
bZezzz
  • 972
  • 9
  • 22
  • should get in habit of using `var` for all your variable declarations... asking for all sorts of problems if you don't – charlietfl Nov 20 '12 at 22:16

2 Answers2

5

I think your problem is that your GetAnswer() function is agnostic to the rest of the application. It doesn't know what was clicked. I think that the easiest thing to do would be to just add the nom parameter to your DisPlay() function call. Like this: (remove the linefeeds)

$('<tr id="bar"></tr>').html('<th class='+Col0+' data-item='+Col0+'>'+Col0+'</th>    <td onclick="DisPlay('+Col1+',\"'+Col0+'\")">'+Col1+'€</td>    <td onclick="DisPlay('+Col2+',\"'+Col0+'\")">'+Col2+'€</td>    <td onclick="DisPlay('+Col3+',\"'+Col0+'\")">'+Col3+'€</td>').appendTo('#pizzas');

Then your DisPlay(Figure) becomes:

function DisPlay(Figure, nom) {
    var Display = document.getElementById('Display');
    if (Figure === null) {
        Display.value = Figure;
    }
    else 
    {
        Display.value += "+"+Figure;

        //instead of this --> //GetAnswer();

        //in your old code you had some of these variables declared as non-global variables, but were using them like they were global variables
        Screen = Display.value.replace(/'/g, ' ');
        result = eval(Screen);   
        Display.value = result;
        $('.theList').append('<li>Ajouté: '+nom+'</li>');
    }
    Figure = null;
}

A word about global variables vs. non-global variables. If you 'var' something inside a function, that variable is local to that function. It seems like you were mixing and matching your vars. For more on that read this post.

EDIT:

After your new edits you are only getting numeric pizza names because you need to have quotes int he proper place. Try changing all your GetName('+Col0+'); to GetName(\''+Col0+'\');

So that whole line would read:

$('<tr id="bar"></tr>').html('<th class="title">'+Col0+'</th><td onclick="DisPlay('+Col1+');GetName(\''+Col0+'\');">'+Col1+'€</td><td onclick="DisPlay('+Col2+');GetName(\''+Col0+'\');">'+Col2+'€</td><td onclick="DisPlay('+Col3+');GetName(\''+Col0+'\');">'+Col3+'€</td>').appendTo('#pizzas');
Community
  • 1
  • 1
davehale23
  • 4,374
  • 2
  • 27
  • 40
1

actually problem is that your onclick function passing string without quotation, put quotation for both GetName('value') and DisPlay('value') it will solve the problem.

<td onclick="DisPlay('"+Col1+"');GetName('"+Col0+"');">'+Col1+'€</td>

ISSUE:

if you put string without quotation js will consider it variable and through an error of undefined variable while numbers will work because int does not required any quotation:

eg; text

it will execute DisPlay and take 15 as int while on executing GetName(Letter) it will try to find Letter variable which is undefined while GetName('Letter') will execute.

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110