admin管理员组文章数量:1428327
I am trying to pass a variable in javascript. I create a link in the following manner and everything seems to be working.
label.innerHTML = '<a href="#" onclick="show_box(this);"> link</a>';
However when I create the link in the following way where the link would also pass an associated object I get the following error from firebug -> "missing ] after element list"
label.innerHTML = '<a href="#" onclick="show_box(this,'+object+');"> link</a>';
Is this an acceptable way to pass an object to a function. The problem is that I am creating this link within a function. The function creates links like this based upon an object that is passed to it. Therefore I cannot have this "object" as a global scope.
I am trying to pass a variable in javascript. I create a link in the following manner and everything seems to be working.
label.innerHTML = '<a href="#" onclick="show_box(this);"> link</a>';
However when I create the link in the following way where the link would also pass an associated object I get the following error from firebug -> "missing ] after element list"
label.innerHTML = '<a href="#" onclick="show_box(this,'+object+');"> link</a>';
Is this an acceptable way to pass an object to a function. The problem is that I am creating this link within a function. The function creates links like this based upon an object that is passed to it. Therefore I cannot have this "object" as a global scope.
Share Improve this question asked May 18, 2011 at 17:27 slimboslimbo 2,7594 gold badges26 silver badges36 bronze badges3 Answers
Reset to default 7You are building the script by mashing together strings, as such you can only work with strings and object
will be automatically stringified.
Use DOM instead.
var link = document.createElement('a');
link.href = "#"; // Have a more sensible fall back for status bar readers and middle clickers
link.appendChild(document.createTextNode(' link');
link.addEventListener('click',function () { show_box(this, object); },false);
label.appendChild(link);
… but use a library that abstracts away the non-standard event models that some browsers have.
What you're trying to do is pass the contents of object
to output. Since it's an object, the string representation will be something like [object Object]
. The output HTML would look like:
<a href="#" onClick="show_box(this, [object Object]);">link</a>
which is invalid. Don't try to concatenate the object, just pass it along as another argument to the function, like this
. Or, better yet, use jQuery:
<!-- somewhere in the head, or at least after the object is defined -->
<script type="text/javascript">
$(function() {
$('#thelink').click(function() { show_box(this, object); });
});
</script>
...
<a href="#" id="thelink">link</a>
If your object is simple variable like numeric or string variable than it will be Ok but if you are passing html object it will not work because it will be something like below.
<a href="#" onclick="show_box(this,[object HTMLDivElement]);"> link</a>
本文标签: htmlPassing Variables JavascriptStack Overflow
版权声明:本文标题:html - Passing Variables Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745523594a2661737.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论