javascript - I just can't figure out how to retrieve the name and value of the object passed -


i have function show, display nodes , attributes of xml. created object attributes store name , value of each attribute. part working well. now, when pass object function cant retrieve name , value of attributes. says aname , avalue undefined. can please help?

my code:

function show(){   :   var mytag2 = child.nodename;   var mytagvalue2 = child.textcontent;   var attributes = [];   (var k = 0; k < child.attributes.length; k++) {      var attrib = child.attributes[k];      if (attrib.specified === true) {         attributes.push({aname: attrib.name, avalue: attrib.value});         $("#maincontent").append("<li>" + attributes[k].aname + " = " +           attributes[k].avalue + "</li>");      }   }    $("#maincontent").append("<td><button id=\"popup\" onclick=\"div_show('" + mytag2 + "','" + mytagvalue2 + "','" + attributes + "')\">edit</button></td><td></td></tr>"); }  function div_show(tag, tcontent, attributes){   (i = 0; < attributes.length; i++) {     var target = document.getelementbyid('attributes');     target.innerhtml = '';     target.innerhtml += "<input id=" + "'" + attributes[i].aname + "'" + " " +     "type = text" + " " + "name=" + "'" + attributes[i].aname + "'" + ">";      document.getelementbyid("'" + attributes[i].aname + "'").value =      attributes[i].aname;     document.getelementbyid("'" + attributes[i].avalue + "'").value =       attributes[i].avalue;         } } 

without getting discussion why should of differently, i'll point out problem, , how fix it.

in code, creating string:

$("#maincontent").append("<td><button id=\"popup\" onclick=\"div_show('" + mytag2 + "','" + mytagvalue2 + "','" + attributes + "')\">edit</button></td><td></td></tr>"); 

attributes array object, , call tostring() method on in order concatenate string being built. render [object object].

you should serialize json, , concatenate instead (also remove quotes around string since want pass array object).

$("#maincontent").append("<td><button id=\"popup\" onclick=\"div_show('" + mytag2 + "','" + mytagvalue2 + "'," + json.stringify(attributes) + ")\">edit</button></td><td></td></tr>"); 

this produce string looks array want pass:

[{"aname":"somename","avalue":"somevalue"}] 

here simplified example shows i'm talking about:

var attributes = [    { aname: 'test1', avalue: 'test1' },    { aname: 'test2', avalue: 'test2' }  ];      var arrayconcatenated = 'array concatenated: ' + attributes;  var arrayjsonconcatenated = 'array json concatenated: ' + json.stringify(attributes);    document.getelementbyid('arrayconcatenated').innerhtml = arrayconcatenated;  document.getelementbyid('arrayjsonconcatenated').innerhtml = arrayjsonconcatenated;
<div id="arrayconcatenated"></div>  vs.  <div id="arrayjsonconcatenated"></div>


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -