1
Hello.
I have this code that is failing to capture this variable.
HTML, attempt 1:
<html>
<head>
<meta charset="utf-8" />
<script src="js.js"></script>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
</head>
<body>
<p><a onclick="funcao()">Clique aqui!</a></p>
</body>
</html>
Javascript (with jQuery), try 1:
var link;
var testando="Testando essa <a href=\""+link+"\">variável</a> aqui.";
function funcao(){
link="http://www.google.com";
$('p').html(testando);
}
HTML, trial 2:
<html>
<head>
<meta charset="utf-8" />
<script src="js.js"></script>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
</head>
<body>
<p><a onclick="funcao('www.google.com')">Clique aqui!</a></p>
</body>
</html>
Javascript (with jQuery), trial 2:
var link;
var testando="Testando essa <a href=\""+link+"\">variável</a> aqui.";
function funcao(link){
$('p').html(testando);
}
In neither of the two, the variable "link" is received, with the value "Undefined" (undefined).
This is the initial way it works (but I’m optimizing):
Try HTML 1;
Javascript (with jQuery):
var link;
var testandoi="Testando essa <a href='";
var testandoii="'>variável</a> aqui.";
function funcao(link){
$('p').html(testandoi+link+testandoii);
}
I find it totally unnecessary to declare two variables for one sentence.
If anyone has a solution, I thank you.
Try to pass the variable when calling the function, without creating the "var link". JS can understand that it is a local variable and does not return the desired.
– Bruno Fonseca