/    Sign up×
Community /Pin to ProfileBookmark

Javascript function to pass array as parameter

Hi guys I am facing problem when passing array as parameter to function. I don’t found any solution that’s why I am here.

Javascript code:

var items = [{“id”:”1″,”name”:”rishi”},{“id”:”2″,”name”:”xyz”}];
var output +=”<button type=’button’ onClick=’print(“+items+”)’>Print</button>”;
$(“body”).append(output);

Html code after run:
<!— it’s coming in function like this [object Object],[object Object] –>
<button type=”button” onClick=’print([object Object],[object Object])’>Print</button>

error in console it’s printing : Uncaught SyntaxError: Unexpected end of input

to post a comment
HTMLJavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJul 20.2019 — Unexpected end of input[/quote]I entered your code into my testpage and I don't get such error. I suspect the error is located somewhere else.
Copy linkTweet thisAlerts:
@SempervivumJul 20.2019 — PS: Besides this syntax error: The function print doesn't accecpt any parameters. It will print the complete page.
Copy linkTweet thisAlerts:
@Rishi69authorJul 20.2019 — Uncaught SyntaxError: Unexpected identifier actually this error is coming when I pass more than one parameter to function like this

var output +="<button type='button' onClick='print("test","+items+","test2")'>Print</button>";

$("body").append(output);
Copy linkTweet thisAlerts:
@JMRKERJul 23.2019 — Not sure this will work either, but it might be closer:
<i>
</i>var output += "&lt;button type='button' onClick='print("test","+items+","test2")'&gt;Print&lt;/button&gt;";
$("body").append(output);


Might want to look into the newer E6 JS actions of spread/rest commands
×

Success!

Help @Rishi69 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...