javascript - jQuery var undefined after 1 loop(variable) -


i running loop, checks div's it's content(its small 4 pieced puzzle, data taken xml files. have variable makes run trough div #1a #4a. use same variable go div, , see if there image same class parent id has. jquery:

        //run when want check answer         $(document).ready(function() {             var correctpositions = 0;              function handleform(count) {                 //build div var                 console.log("clickdetected");                 var div = "#"+ count +"a";                 console.log("this counter "+count);                 console.log("this div im looking into: "+div);                  //check if div has children                 if ($(div).children().length > 0){                     var childid = $(div + ":first-child").attr("id");                     //got html test if there wasn't anything, returns: undefined                     var childhtml = $(div + ":first-child").html();                     console.log(childhtml);                     if(childid != null){                         console.log("child id "+ childid);                         if(count = childid){                             correctpositions++;                             console.log("correct answers:"+correctpositions);                         }                     } else {                         console.log("child undefined");                     }                 }                 console.log("end of if");             }              //bind click function             $("#goed").click(function() {                 //loops 4 times                 for(count = 1; count < 5; count++) {                     handleform(count);                 }                                });          }); 

this html when puzzle pieces on right place.

                <div id="pieceholder">                     <div class="position 1 ui-droppable" id="1a"><span style="display:none;" id="1">1</span></div>                     <div class="position 2 ui-droppable" id="2a"><span style="display:none;" id="2">2</span></div>                     <div class="position 3 ui-droppable" id="3a"><span style="display:none;" id="3">3</span></div>                     <div class="position 4 ui-droppable" id="4a"><span style="display:none;" id="4">4</span></div>                 </div> 

first div fine, finds child span , it's id. only, when second div, still finds parent div, says there no child, , thus, no puzzle piece found. missing?

it has been mentioned ought not start id digit. let that number 4 example. following line:

var childid = $(div + ":first-child").attr("id"); 

would be:

var childid = $("#4a:first-child").attr("id"); 

this not refer first child of #4a if intention. already, #4a returns 1 element; when append :first-child collection returned empty because #4a not it's parent's first-child.

may meant write:

var childid = $(div).children("span:first-child").attr("id"); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -