admin管理员组

文章数量:1300185

I know this is an easy one, but I can't find exactly what I am looking for. I was wondering if it is possible to pass an array into a method as a parameter, but through HTML. If it is possible I think it would be something like this:

<img src="img.png" alt="picture" onclick="thisMethod1(['Facebook','Twitter'])"/>

The javascript would look something like this:

function thisMethod1(socialArray)
{
     //use this array
}

Is this correct?
The reason I am asking is because there is an error in my code and I am trying to find it.

I know this is an easy one, but I can't find exactly what I am looking for. I was wondering if it is possible to pass an array into a method as a parameter, but through HTML. If it is possible I think it would be something like this:

<img src="img.png" alt="picture" onclick="thisMethod1(['Facebook','Twitter'])"/>

The javascript would look something like this:

function thisMethod1(socialArray)
{
     //use this array
}

Is this correct?
The reason I am asking is because there is an error in my code and I am trying to find it.

Share Improve this question asked Aug 1, 2013 at 0:26 KernelSanders213KernelSanders213 1471 gold badge1 silver badge10 bronze badges 2
  • Post the real code and someone might find the problem. I suspect this question will get downvoted and maybe put on hold as it stands. – user2625787 Commented Aug 1, 2013 at 0:30
  • What error are you getting? – Mike Brant Commented Aug 1, 2013 at 0:32
Add a ment  | 

2 Answers 2

Reset to default 5

Yes it should work, as this demonstrates:

<script>
function thisMethod1(socialArray)
{
    alert(socialArray);
}
</script>

<img src="img.png" alt="picture" onclick="thisMethod1(['Facebook','Twitter'])"/>

If the image is inside a link, then clicking it might cause the link to submit, so that may be causing you to think there's an error in your code.

Yes, though best practice says not to use inline code (in the tag).

本文标签: Pass array in HTML method parameters javascriptStack Overflow