How to set attributes in dynamically creating table structure using javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ammu86
    New Member
    • Jan 2010
    • 6

    How to set attributes in dynamically creating table structure using javascript?

    Im developing a script for dynamically creating html table structure so that from the input that has been given to the function it will fetch the values one by one and will create the structure. My problem is i want to add attributes to the elements created. sample code attached.
    Code:
    with( window['o' + elementname]){
    for(j=0;j<attrs.length;j++)
    {
    if(attrs[j].indexOf("=")!=-1){
    var attr1={}
    attr1=attrs[j]
    attr1=attr1.split("=")
    [B] window[attr1[0]]=attr1[1];[/B]
     }
     }}
    here the element name will be get from the parameters passed to the function. i want to verify whether the line in bold is correct or not coz the id is not updated in output.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Can you not use setAttribute()? Can you post a sample input?

    Comment

    • ammu86
      New Member
      • Jan 2010
      • 6

      #3
      The parameter that i passed to the function will be in the below format.
      var attr = {node:"<tr>,id= tr1$<td>,id=td1 ,colspan:2,styl e.width=100%$<d iv>,id=oWaiter, name=oWaiter,st yle=position:re lative,border:1 px gray solid,overflow: hidden,width:20 0px,height:15px ,background-color:#FFF;"}


      for eg: when the value style.width=100 % passed to parameter it should assign to the element. If i use setattributes instead of 'style=position :relative' it will return 'style=position =relative'. which is not correct.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Interesting and strange. Any particular reason why it's in this format? Could you not use XML or JSON or at least make it easier to use? That's not to say it's impossible or even difficult, but it just adds complexity where it's not required.

        The $ character seems to be the separator. Once you split on that, you can split again on the comma to get the tag, e.g. <tr>. Then you can split again on the = to get attribute/value pairs.

        Unfortunately, it seems there's some inconsistency,e .g. "colspan:2" instead of "colspan=2" .

        Comment

        Working...