Results 1 to 2 of 2

Thread: Accessing dynamic div based off array.

  1. #1
    Join Date
    Dec 2011
    Posts
    34
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Accessing dynamic div based off array.

    edit again: Tried adding onclick to the div when it's created, but have been having trouble getting it working right. Currently:
    Code:
    pack.innerHTML += '<div id="'+i+'" class="clickable" onclick="rangerItem('+ i +'); Fn('+ i + ":" + playerRanger[i]+'); ">'+ i +" <br />"+playerRanger[i]+'</div>';
    
    var drophere;
    function rangerItem(i){	
    console.log('rangerFired:'+i);
    		drophere=document.getElementById('drop1');	
    		drophere.innerHTML= i ;
    		console.log(playerRanger[i]);
    		console.log(i);
    		}
    
    function Fn(i){
    	console.log('Fn: '+i);	
    	}
    This gives an error when I click the different divs
    missing ) after argument list
    [Break On This Error] rangerItem(ranger_boots); Fn(ranger_boots:1);
    The error has the right properties as parameters, I clicked ranger_boots div, and the 1 is how many I had when I clicked the icon.

    Anyone have any ideas?????


    Here's the context of it, I deleted it when I edited, but maybe it makes more sense seeing the entire function:
    Code:
    	
    for (var i in playerRanger){   //for each object in array
     	 if(playerRanger[i] > 0){      //if the number of objects in array is 1 or more (if player has one)
    
    	 if(document.getElementById("pack")!= null){		
    			
    	pack.innerHTML += '<div id="'+i+'" class="clickable" onclick="rangerItem('+ i +'); Fn('+ i + ":" + playerRanger[i]+'); ">'+ i +" <br />"+playerRanger[i]+'</div>';			
        
    		$(function() {
    		$( ".clickable" ).draggable(); });	
    		}
    		}
    
    	}
    Last edited by lmbarns; 01-22-2012 at 01:12 AM.

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,793
    Thanks
    2
    Thanked 418 Times in 412 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <style type="text/css">
    /*<![CDATA[*/
    .clickable{
      width:100px;height:100px;background-Color:red;
    }
    
    /*]]>*/
    </style></head>
    
    <body>
    <div id="tst" ></div>
    
    <script type="text/javascript">
    /*<![CDATA[*/
    playerRanger=[];
    playerRanger[0]='tom';
    playerRanger[1]='joe';
    
    for (var i=0;i<playerRanger.length;i++){
     document.getElementById('tst').innerHTML += '<div id="'+i+'" class="clickable" onclick="rangerItem('+ i +');Fn('+i+',\''+playerRanger[i]+'\');" >'+i+'<br>'+playerRanger[i]+'</div>';
    }
    
    var drophere;
    function rangerItem(i){
     alert(i);
    }
    
    function Fn(i,r){
     alert('Fn: '+i+':'+r);
    }
    
    /*]]>*/
    </script>
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. The Following User Says Thank You to vwphillips For This Useful Post:

    lmbarns (01-22-2012)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •