2
My function being called during onclick event is not being recognized, this is the console error:
ReferenceError: abrirfoto is not defined
HTML(only relevant code):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Mycode</title>
<script src="Javascript/javascript.js" type="text/javascript"></script>
</head>
<body>
<div id="layout">
<main>
<section>
<ul id="dpf">
<li onclick="abrirfoto(this);"><img src="Backgrounds/Foto1.jpg"></li>
<li onclick="abrirfoto(this);"><img src="Backgrounds/Foto2.jpg"></li>
<li onclick="abrirfoto(this);"><img src="Backgrounds/Foto3.jpg"></li>
</ul>
</section>
</main>
</div>
</body>
Obs:in integer code the attribute "alt" is used in images.
Javascript
function abrirfoto(myElement) {
myElement.style.width = 300px;
}
I tried to put my function inside a script tag of my Html5, but the same error persists.
Actually this is exactly what I had done before,but it seems that when I use the "onlclick" for a function the "this" within the function already refers to the element that called the function. I don’t remember where I had read this (or even if I was confused)but can you confirm it for me? And that dot and comma at the end of the "onlclick" is necessary?
– user48589
I added an explanation regarding your doubt of using the
this
, and as to the;
, it is necessary to call more than one method within theonclick
in the HTML– Brumazzi DB
Thank you friend! D
– user48589