I have this navBar:
<?= Nav::widget([
'items' => [
[
'label' => ...
'url' => ...
'active' => ...
'linkOptions' => array('onclick'=>'setButtonValue("stringValue")'),
],
When I click this Nav option I want to set the value of a button of the same page with "stringValue"
<button class="btn btn-primary" type="button" id="buttonQuestion" value="">
<i class="fas fa-question"></i>
</button>
At the end of the page I'm registering the function:
$this->registerJs(
"function setButtonValue(stringValue) { alert('Button clicked!'); });",
);
?>
But In the console I got the following error;
setButtonValue is not defined
Am I using a wrong approach ?
You have a wrong ) in your js code
remove the )
after closing }
function setButtonValue(stringValue) { alert('Button clicked!'); });,
^^ here
.
$this->registerJs(
"function setButtonValue(stringValue) { alert('Button clicked!'); };",
);
?>
You can do this way too:
NavBar
...
'linkOptions' => array('onclick'=>'$(this).html("stringValue");'),
...
Here is the Example
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <li onclick="$(this).html('Changed');">Text</li>
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.