views:

27

answers:

1

Hi,

I have a 'for' loop which extracts data from an XML document and adds it into some JavaScript objects, each time the loop executes I want it to run a certain function depending on the value of the attribute 'typ' which is being retrieved from the xml.

Currently, the data from the XML is successfully being parsed, which is proven by the first 'alert' you can see which produces the correct value.

However, neither of the 'alert' lines in the 'if' statement lower down are being executed and as a result I cannot test the 'ctyp3' function which is supposed to be called.

Where have I gone wrong?

                      for (j=0; j<arrayIds.length; j++)
                  { 
                    $(xml).find("C[ID='" + arrayIds[j] + "']").each(function(){
                        // pass values
                        questions[j] = {
                            typ: $(this).attr('typ'),
                            width: $(this).find("I").attr('wid'),
                            height: $(this).find("I").attr('hei'),
                            x: $(this).find("I").attr('x'),
                            y: $(this).find("I").attr('x'),
                            baC: $(this).find("I").attr('baC'),
                            boC: $(this).find("I").attr('boC'),
                            boW: $(this).find("I").attr('boW')
                        }

                        alert($(this).attr('typ'));

                        if ($(this).attr('typ') == '3')
                        {
                            ctyp3(x,y,width,height,baC);
                            alert('pass');
                        } else {
                            // Add here
                            alert('fail');
                        }
                    });
                  }

EDIT: After your suggestions I commented the ctyp3 function and the if statement proceeded to execute correctly.

Here is the ctyp3 function:

                      function ctyp3(x,y,width,height,baC)
                  {
                      alert('alsdjkfs');
                      document.createElement('rect');
                      this.x = x;
                      this.y = y;
                      this.width = width;
                      this.height = height;
                      this.fillcolor = baC;
                      svgTag.appendChild(this);
                  }

I was attempting to create an object and then a html element, something I have not done before.

+2  A: 

The "this" in your ctyp3 function is not going to be what you think it is. You need to assign the result of document.createElement to a variable:

var r = document.createElement('rect');

Then use "r" instead of "this". Similarly, it's not clear what "svgTag" is; if it's not a global variable then you're going to have to pass it to the function explicitly.

Pointy