Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] Javascript and I have different ideas about what "concatenate" means.



Hello Dave,

I can't exactly figure out what's wrong with your Javascript code, but
note that if you are building a valid html document there must be only
one element with a certain id ('class' is the best fit for what you
are trying to do)

I guess there's some kind of html validation when using .innerHTML
which tries to overcome the multiple id problem...Maybe you can try to
alert() the output of your code before passing it to innerHTML
property to see if it's already modified or not.

Regards,

Marco


2010/12/15, Dave M G <dave@example.com>:
> TLUG,
>
> Me and Javascript are disagreeing, and I'm sure I'm right about this.
>
> I need to output a sequence of nested <div> tags. The desired output is
> this:
>
> <div id="1">
>    <div id="2">
>         <div id="3">
>         </div>
>     </div>
> </div>
> <div id="1">
>    <div id="2">
>         <div id="3">
>         </div>
>     </div>
> </div>
>
> To do that, I've got my code set up like this:
> output = "";
> for (var key in theVariableThatIsBeingUsedForThis)
> {
>     output = output.concat('<div id="1">\n\
>    <div id="2">\n\
>         <div id="3">\n\
>         </div>\n\
>     </div>\n\
> </div>\n\')
> }
> document.getElementById("containerDiv").innerHTML = output;
>
> What I'm getting from this is:
> <div id="1">
>    <div id="2">
>         <div id="3">
>             <div id="1">
>                 <div id="2">
>                     <div id="3">
>                     </div>
>                 </div>
>             </div>
>         </div>
>     </div>
> </div>
>
> So it's as if the concat() function in Javascript is somehow looking at
> the tree of div tags and adding it onto the outermost branch, instead of
> doing what I wanted it to do, which is just treat it like a string and
> append it to the end.
>
> What the heck? Either I've got some bug that I don't see, or the people
> who have Javascript have a completely different idea of than I do of
> what it means to concatenate strings.
>
> Any ideas on what the heck is going on here? Am I wrong about what I
> should expect from the way I've set up my code?
>
> Any advice would be much appreciated.
>
> --
> Dave M G
>
> --
> To unsubscribe from this mailing list,
> please see the instructions at http://lists.tlug.jp/list.html
>
> The TLUG mailing list is hosted by the award-winning Internet provider
> ASAHI Net.
> Visit ASAHI Net's English-language Web page: http://asahi-net.jp/en/
>


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links