{"id":430,"date":"2022-08-30T15:01:13","date_gmt":"2022-08-30T15:01:13","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/pass-generated-string-as-variables-in-javascript-function-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:01:13","modified_gmt":"2022-08-30T15:01:13","slug":"pass-generated-string-as-variables-in-javascript-function-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/pass-generated-string-as-variables-in-javascript-function-collection-of-common-programming-errors\/","title":{"rendered":"Pass generated string as variables in javascript function-Collection of common programming errors"},"content":{"rendered":"<p>This is my html:<\/p>\n<pre><code>\n\n\n\n\n\n\n\n\n<\/code><\/pre>\n<p><strong>I created a function to make a string and pass it to another function<\/strong> <strong>The string contain all values for input text with match string &#8216;campos&#8217; in the input name.<\/strong><\/p>\n<pre><code>\nvar tags_inpt = new Array();\nvar param = \"\";;\nfunction inpt() {\ntags_inpt=document.getElementsByTagName('input');\nvar i;\nfor (i=0; i \"valor0\",\"valor1\",\"valor2\",\"valor3\",\"valor4\",\"valor5\",\"A\" OK!!\n\/\/ call funcion2()\nfuncion2(\"valor0\",\"valor1\",\"valor2\",\"valor3\",\"valor4\",\"valor5\",\"A\"); \/\/ this result in valor1 in funcion2() OK!!\nfuncion2(param + '\"A\"'); \/\/ this return 'undefined' --&gt; BAD\n}\n\nfunction funcion2(a,b,c,d,e,f,g){\nvar z = b;\nalert (z);\n}\n\n<\/code><\/pre>\n<p>When use param variable to dynamically pass arguments to <code>funcion2()<\/code>, I get undefined value.<\/p>\n<p>What is the correct way to make this?<\/p>\n<p>Thanks<\/p>\n<ol>\n<li>\n<p>Try this:<\/p>\n<pre><code>funcion2.apply(window, (param + ',\"A\"').split(\",\") );\n<\/code><\/pre>\n<p>See the <strong>DEMO<\/strong><\/p>\n<\/li>\n<li>\n<p>Don&#8217;t use string concatenation for this, that&#8217;s prone to errors depending on your input. Use an array instead:<\/p>\n<pre><code>\nfunction inpt() {\n  var tags_inpt = document.getElementsByTagName('input');\n  var matches = [];\n  for (var i=0; i<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:41:57. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>This is my html: I created a function to make a string and pass it to another function The string contain all values for input text with match string &#8216;campos&#8217; in the input name. var tags_inpt = new Array(); var param = &#8220;&#8221;;; function inpt() { tags_inpt=document.getElementsByTagName(&#8216;input&#8217;); var i; for (i=0; i &#8220;valor0&#8243;,&#8221;valor1&#8243;,&#8221;valor2&#8243;,&#8221;valor3&#8243;,&#8221;valor4&#8243;,&#8221;valor5&#8243;,&#8221;A&#8221; OK!! \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-430","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/430","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=430"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/430\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}