Why am I getting [object HTMLImageElement]?
I'm having some problems to pass string variable to a method RequirementPopup. This method expects three arguments id, typeId and objType (type of an object, it coult be "Item" for example)
First method where is the button which executes other method where three variables are being passed.
function EditForm(count, id, typeId, nr, objType, name, lat, lon) {
/* Here is something else */
<p><input type="button" id="edit-requirement" value="Edit requirement" onclick="javascript:RequirementPopup('+id+','+typeId+','+objType+')"/></p>
}
Second method which suppose to display our variables which were passed:
function RequirementPopup(id, typeId, objType) {
/* Here is something else */
document.getElementById("id").value = id;
document.getElementById("tid").value = typeId;
document.getElementById("oType").value = objType;
}
In div id and tid I am getting right variables but in oType I am getting [object HTMLImageElement] rather than "Item"?
P.S When I display objType in EditForm method for instance in alert it is correct but when I pass it to RequirementPopup method I'm getting something like [object HTMLImageElement]
Could anyone tell me why it is h开发者_开发问答appening and how to solve that problem?
You're not putting any quotes around the id, typeId and objType values in the JavaScript expression that you're generating in EditForm. If id and typeId happen to be numeric values, then they come out as expected because these numbers are interpreted in the JavaScript expression that calls RequirementPopup as numbers. But if objType is a name like "Item", then the JavaScript expression that calls RequirementPopup is going to interpret it as a symbol, not a string, and the argument that gets passed to RequirementPopup will be whatever is named by that symbol.
Try this:
function EditForm(count, id, typeId, nr, objType, name, lat, lon) {
/* ... */
var inputHtml =
'<p><input type="button" id="edit-requirement" value="Edit requirement" onclick="' +
'javascript:RequirementPopup(' + id + ',' + typeId +',' +
"'" + objType + "'" +
')"/></p>';
}
精彩评论