Reputation: 75
here is the code:
$(document).ready(function(){
var Jstr = { "JSON" : [
{ "Eid" : 102,"Ename":"", "Ecode" : "<input type ='text'/>", "Eprops": {"name": "", "value":"", "maxlength":""}}
]
} ;
$(".search").click(function(){
var id = this.id;
for(var i =0;i<Jstr.JSON.length;i++)
{
var obj = Jstr.JSON[i] ;
if(obj.Eid == id)
{
document.getElementById('down').innerHTML=document.writeln(obj.Ecode);
}
}
});
});
Here I'm using JSON object to create a input type = text tag by clicking a button on the page. Then I'm trying to create that tag in a particular div. My main focus is to create the input tag in a "DIV" of id "down" using document.writeln() method. ".search" is the class of the button which is clicked.
Upvotes: 1
Views: 1789
Reputation: 157
I think u want to do something like this and is better to use jsfidle for ur problem sol1
var tempId = document.getElementById('down');
tempId.innerHTML=obj.Ecode;
and sol2
$('#down').append(obj.Ecode);
Upvotes: 1
Reputation: 106385
First, please don't ever use document.write
unless you really really know what you're doing. There are possible scenarios when that method makes sense, but they are quite rare in the real world.
Second, if you're employing jQuery - well, you can use jQuery for this particular task as well. ) Compare this line...
document.getElementById('down').innerHTML = obj.Ecode;
... with this:
$('#down').html(obj.Ecode);
That's if you want to completely rewrite the contents of <div id="down">
each time. If what you actually want is just to add a new element, use .append
:
$('#down').append(obj.Ecode);
... or, if you want to add those properties stored in Eprops
:
$('#down').append($(obj.Ecode, obj.Eprops));
Upvotes: 3
Reputation: 2907
try this:
document.getElementById('down').innerHTML=obj.Ecode;
Upvotes: 0