html - can we use a variable to declare a div in JAVASCRIPT? -


i working in phone gap-android.i wanted set swipe view dynamically based on length of records.

how do that? after long time,i tried implementing following code.am right or wrong?

value= value_from_db.split("||");             (k=0;k<value.length;k++)             {                 if (value[0] == paramname1)                 {                       return unescape(value[k]);                       console.log("no of swipe views ");                   }                    var val = k+1;                   var ni = document.getelementbyid('swiper-wrapper');                   var newdiv = document.createelement('div');                   var dividname = 'swiper-slide'+val;                   console.log("div name: "+dividname);                   newdiv.setattribute('id',dividname);                   newdiv.setattribute('class','swiper-slide');                   var cnt1 = '<div id="container'+val+'"><span><img src="img/abc'+val+'.png" style="float:left; " /></span><div id="abc'+val+'"><span><h5>'+value[k]+'</h5></span></div></div>';         ---->     console.log("div_id :"+id);                   document.getelementbyid(+dividname).innerhtml=cnt1;                   console.log("value_from_db:: "+value[k]);                   ni.appendchild(newdiv); 

while, trying execute,that value not print , see error reference error,id not defined andit blank screen div has viewed.

in html code,i have given following,

<div id="swipe_body">          <div class="swiper-container swiper-threshold">             <div class="swiper-wrapper">              </div>         </div>     </div> 

is possible or not? have committed mistake?

after "@nnnnnn" suggestions have changed , implemented following code:

 var cnt1 = '<div id="container'+val+'"><span><img src="img/abc_'+val+'.png" style="float:left; " /></span><div id="abcd'+val+'"><span><h5>'+value[k]+'</h5></span></div></div>';                  // console.log("div_id :"+id);                   document.getelementbyid(dividname).innerhtml=cnt1;                   console.log("abcd values: "+value[k]);                   ni.appendchild(newdiv); 

but,i getting following error:

05-14 17:24:25.382: i/web console(17882): jscallback error: typeerror: cannot set property 'innerhtml' of null @ file:///android_asset/www/cordova-2.1.0.js:3727 

in order bottom of this, you'd need check:

  1. what 'dividname' set to
  2. if 'swiper-slide(dividname)' exists @ time execute script

the error says you're trying set innerhtml of nothing, in turn means element you're trying select doesn't exist.

if console.log variables can out better.

as side note: should clean code , hoist variables, it's hard debug spaghetti code ;-)


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -