When I click on a table cell, i want my program to run a function whilst passing the parameter $venues[$y][1]
through. However, whenever i click on a cell, i get the error:
SyntaxError: expected expression, got '}'
Here is the function which gets called:
<script type="text/javascript">
function displayPicture(x){
alert(x);
}
</script>
Here is the line that calls the function (its html and php):
echo '<td class="venueNames" onclick="displayPicture("'.$venues[$y][1].'")">'.$venues[$y][1].'</td>';
For the sake of this example, lets assume $venues[$y][1]
is Wembley.
Is the issue because I am passing a php variable as a parameter for a javascript function?
You have problably a problem with masking your quotation marks. Try the following:
echo '<td class="venueNames" onclick="displayPicture(\''.$venues[$y][1].'\')">'.$venues[$y][1].'</td>';
or
echo "<td class=\"venueNames\" onclick=\"displayPicture('{$venues[$y][1]}')\">{$venues[$y][1]}</td>";
In your original code the onclick
-attribute will be seen by the browser as onclick="displayPicture("
- ie the second double quotation mark is seen as the end of the onclick attribute and not as an internal quotation mark enclosing a string in your function call.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.