Element creation in for() loop

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1711 Views - Last Post: 19 February 2012 - 03:47 AM

#1 manjukakkadath   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-February 12

Element creation in for() loop

Posted 14 February 2012 - 04:01 AM

in the below code i need to traverse each value of object and create html dynamically.
var attr = {"<tr>":"id=tr1","<td>":"id=td1;value=some text here"}

   for ( var key in attrs) { var currentattr=attrs[key]
            if(currentattr.indexOf(";")>-1){
                currentattr=currentattr.split(";")
                jQuery.each(currentattr, function(i, val) {                       
                    var currattr1=val.split("=")
                    var currentattrname,currentattrvalue;
                    currentattrname=currattr1[0]
                    currentattrvalue=currattr1[1]
                    if(currentattrname=="value"){                    
                     $(key).append("some text here ...")
                     }
})
}
}

here im trying to split the input by using split and adding each node to the root to create dynamic html content. I am new to jquery. Please review the code and let me know any modification will make the code more flexible using jquery features.

Is This A Good Question/Topic? 0
  • +

Replies To: Element creation in for() loop

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: Element creation in for() loop

Posted 14 February 2012 - 04:08 AM

View Postmanjukakkadath, on 14 February 2012 - 12:01 PM, said:

var attr = {"<tr>":"id=tr1","<td>":"id=td1;value=some text here"}

er that is more than attributes. and since IDs must be unique, it doesn't qualify as template either.

a more sensible approach would be
var elems = {
  tr : {
    id : "tr1"
  },
  td : {
    id : "td1",
    value : "some text here"
  }
}

Was This Post Helpful? 1
  • +
  • -

#3 manjukakkadath   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-February 12

Re: Element creation in for() loop

Posted 14 February 2012 - 06:02 PM

ok..Thanks for providing me a better approach.
But how to traverse through this object?
for eg:I have the below function in which i need to compare "ElmType" to each attribute of the object and if it is present in the object i need to write the code accordingly.

function WriteElement(ElmPaernt,[b]ElmType[/b],ElmID,ElmName,ElmLabel,Elmstyle,ElmDefault,ElmEvent,isChecked) {
    var container = $("#"+ElmPaernt); 
var html = '<input type="'+EmdType+'" id="cb'+EmdID+'" checked="'+isChecked+'" />';
   container.append($(html));
}


ie: so instead of adding attributes of each element manually i can add it by finding the attributes of "elmtype" from the object "elems"

var [b]elems [/b]= {
	  checkbox : {
	    id : "chk1",
            checked:"checked"
	  },
            textbox:{
	    id : "txt1",
            value:"text"
	  }	  
	}

This post has been edited by Dormilich: 14 February 2012 - 06:38 PM
Reason for edit:: removed self-quote

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: Element creation in for() loop

Posted 14 February 2012 - 06:44 PM

I think you try to put too much into a function here.

var attr = {
    type: "checkbox",
    id: "chk1",
    checked: "checked"
}

// and then using a function
$parent.append(makeElement("input", attr));

Was This Post Helpful? 0
  • +
  • -

#5 manjukakkadath   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-February 12

Re: Element creation in for() loop

Posted 14 February 2012 - 06:56 PM

sorry..i am not clear what you said.can you please give me a detailed explanation?

var elems= {
	      checkbox : {
              id : "chk1",
	      checked:"checked"
	      }  
	    }

how to access the value of id from the above object? so that i can refer the object attributes in makeelement.

This post has been edited by Dormilich: 14 February 2012 - 06:57 PM
Reason for edit:: please use [CODE] [/CODE] tags when posting code

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: Element creation in for() loop

Posted 14 February 2012 - 06:58 PM

like you would do on any standard Javascript object.
Was This Post Helpful? 0
  • +
  • -

#7 manjukakkadath   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-February 12

Re: Element creation in for() loop

Posted 14 February 2012 - 07:08 PM

in javascript
elems.Checkbox.id
will give the result. But its not working in jquery.
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: Element creation in for() loop

Posted 14 February 2012 - 07:40 PM

mind that Javascript is case-sensitive. the above call wouldn’t work.
Was This Post Helpful? 0
  • +
  • -

#9 manjukakkadath   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-February 12

Re: Element creation in for() loop

Posted 14 February 2012 - 07:52 PM

 var elems ={
	     Checkbox : {
	    id : "chk1",
	    value : "checked"
	  }
	  
	}

alert(elems.Checkbox.id)

is giving me the value "chk1"
i just want to know how to do the same in jquery.
Please help.
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: Element creation in for() loop

Posted 14 February 2012 - 07:53 PM

that’s a different code you posted now …

I don’t understand what you mean by "do the same in jQuery". jQuery *is* Javascript.

This post has been edited by Dormilich: 14 February 2012 - 07:54 PM

Was This Post Helpful? 0
  • +
  • -

#11 manjukakkadath   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-February 12

Re: Element creation in for() loop

Posted 14 February 2012 - 08:40 PM

in jquery i had seen lot of functions like
content.find
and
object.get
. But none of them are working here.
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: Element creation in for() loop

Posted 14 February 2012 - 08:43 PM

obviously, because they are not intended for your purpose. just use the Javascript syntax.
Was This Post Helpful? 0
  • +
  • -

#13 manjukakkadath   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-February 12

Re: Element creation in for() loop

Posted 14 February 2012 - 08:53 PM

why contents().find("elems").find("Checkbox").attr("id")

is not working?
Was This Post Helpful? 0
  • +
  • -

#14 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: Element creation in for() loop

Posted 14 February 2012 - 08:59 PM

do you know what .find() is supposed to do?
Was This Post Helpful? 0
  • +
  • -

#15 manjukakkadath   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-February 12

Re: Element creation in for() loop

Posted 14 February 2012 - 10:08 PM

it will search for the elements mentioned in the paranthesis.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2