最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Passing dynamic parameter to a JavaScript function using innerHTML - Stack Overflow

programmeradmin0浏览0评论

I am having issues passing a dynamic parameter to a JavaScript function using innerHTML.

Included below is the current code that I am using:

var name = "test";

frm.innerHtml = '<button name="close" id="close" title="Cancel" type="button"
     onclick="closeTab('+name+');">Return</button>';

When I debug the code of the closeTab() function, the parameter specified by the name variable is null.

I believe there is a problem with the declaration of the value while modifying the innerHTML property.

Any help would be greatly appreciated.

Thanks

I am having issues passing a dynamic parameter to a JavaScript function using innerHTML.

Included below is the current code that I am using:

var name = "test";

frm.innerHtml = '<button name="close" id="close" title="Cancel" type="button"
     onclick="closeTab('+name+');">Return</button>';

When I debug the code of the closeTab() function, the parameter specified by the name variable is null.

I believe there is a problem with the declaration of the value while modifying the innerHTML property.

Any help would be greatly appreciated.

Thanks

Share Improve this question edited Jul 4, 2012 at 3:21 Robert 8,7672 gold badges28 silver badges34 bronze badges asked Jul 4, 2012 at 3:03 CoderCoder 3,1308 gold badges52 silver badges86 bronze badges 3
  • do u mean i simply use the variable name without quotes? Thanks – Coder Commented Jul 4, 2012 at 3:08
  • you used the value of name as a variable, add quotations. – Novak Commented Jul 4, 2012 at 3:09
  • You should use DOM like document.createElement and others instead of setting innerHtml because it's cleaner – Alvin Wong Commented Jul 4, 2012 at 3:23
Add a comment  | 

5 Answers 5

Reset to default 6

Your dynamic declaration is passing the parameter as a variable, instead of the value of the parameter. In order to correct this issue, you must pass the value as a string, instead of a variable, which is accomplished by encasing and escaping the variable in single quotes as demonstrated below:

var name = "test";

frm.innerHtml = '<button name="close"  id="close"  title="Cancel" type="button"
      onclick="closeTab(\''+name+'\');">Return</button>';

Your resulting code is:

<button ... onclick="closeTab(test);">Return</button>

Can you see what's wrong? test is being treated as a variable, when you actually intended it to be a string.

I like to use JSON.stringify to make a variable parseable (kind of like var_export in PHP), but in this case you also need to escape quotes. So:

JSON.stringify(test).replace(/"/g,"&quot;")

Use that in your button instead of just test.

Using DOM:

var name="test";
var el = document.createElement("button");
el.name = "close";
el.id = "close";
el.title = "Cancel";
el.type = "button";
el.onclick = function() {  // an inline function that will be executed when el is clicked
    closeTab(name);        // `name` here contains `"test"`
};
frm.appendChild(el);

I really don't encourage overusing innerHTML as it is messy, and can sometime be slow.

Assuming your html is -

<form id='frm'>

</form>

Your JS should be -

var name = "test";
var innerHtml = "<button name='close' id='close' title='Cancel'   type='button'>Return</button>";
$("#frm").html(innerHtml);
$("#close").attr("onclick","javascript:closeTab('"+name+"');");

Check this out - http://jsfiddle.net/arindam006/avgHz/1/

This is the cleanest way to achieve this, though innerHtml is not a proper way to do it as everyone suggested above.

I simply used onclick="closeTab(name);"> in innerHTML and it worked. I guess that worked coz name is global variable.

发布评论

评论列表(0)

  1. 暂无评论