How to implement a closure in jQuery [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I don't know much about jQuery. Here I am trying to implement a closure to attach a click event to each anchor tag so that each of them will alert a certain number once blicked. But I am getting the following error:
SyntaxError: missing ) after argument list
};})(i);
can anyone help me with this problem?thanks :)
Code:
<div id='pageBox'>
<ul id='pageContainer'>
<?php for($i=1;$i<=$pagenum;$i++){?>
<li><a href=''><?php echo $i;?></a></li>
<?php }?>
</ul>
</div>
<script>
for(i=1;i<=<?php echo $pagenum;?>;i++){
$('#pageContainer li a').click((function(i){return function(event){
event.preventDefault();
alert(i);
};})(i);
);
}

You don't need closure here. This can be achieved using simple approach. Persist php variable $i in data-* prefixed custom attribute, which can be fetch using .data()
Change your HTML as
<ul id='pageContainer'>
<?php for($i=1;$i<=$pagenum;$i++){?>
<li><a href='' data-id='<?php echo $i;?>'><?php echo $i;?></a></li>
<?php }?>
</ul>
Script
$('#pageContainer li a').click(function(){
alert($(this).data('id'));
});

Related

SyntaxError: missing ) after argument list php javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am working on a PHP project and I need to echo the dynamic confirmation text. I am trying with following code:
<a href="#" onclick='return confirm(<?php echo $instance->translator->__("Are you sure you want to delete this video?"); ?>)'> Remove</a>
Error:
SyntaxError: missing ) after argument list
how can I solve this ?
You need quotes in the JS/HTML too
<a href="#"
onclick="return confirm('<?php echo $instance->translator->__("Are you sure you want to delete this video?"); ?>')"> Remove</a>
Alternative
<? $areyousure = $instance->translator->__("Are you sure you want to delete this video?"); ?>
<a href="#"
onclick="return confirm('<?= $areyousure ?>')"> Remove</a>
Even better:
<div id="linkContainer">
Remove
</div>
<script>
const areyousure = '<? $instance->translator->__("Are you sure you want to delete this video?"); ?>';
document.getElementById('linkContainer').addEventListener('click', function(e) {
const tgt = e.target;
if (tgt.classList.contains('remove')) {
if (confirm(areyousure)) removeSomething(tgt.dataset.id)
}
});

event on click not working in echo php [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
How to echo in php?
<div class="city" onclick="openCity(event,'London')">
I am try write sintax :
echo ' < div class=" city " onclick=" openCity(event,'London') " > ';
But event onclick not working
You have an issue with your quotes. You must escape the ones that suround your "London" arguement. Try this instead:
echo ' < div class=" city " onclick=" openCity(event,\'London\') " > ';
You might getting javascript syntax error due to single quote. Please you started echo with single quote. So need to handle inner single quotes. You can handle it in two ways
echo '<div class=" city " onclick="openCity(event,\'London\') ">';
or
echo "< div class=\" city\" onclick=\" openCity(event,'London');\" >";

Div tag content not showing on click [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to display the data of a div tag on a href click, the id is dynamically made. But the div tag is not responding. The sequence of the code is as below. The file is the .tpl file.
function editPickUpAddress(divId)
{
("#pickupAddressForm_"+divId).show();
}
<a href style="float:right;margin-top:-20px;margin-right:100px;" onclick="editPickUpAddress({$item.profile_id});">Edit</a>
<div id="pickupAddressForm_{$item.profile_id}" style="display:none">
//some content to display
</div>
After cleaning up your snippet and including jQuery, the code works as intended. Also note the href="#" which prevents the anchor tag from navigating to another page.
function editPickUpAddress(divId) {
$("#pickupAddressForm_" + divId).show();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Edit
<div id="pickupAddressForm_1" style="display:none">
//some content to display
</div>

insert text into span class using jquery [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have following html structure
<a id="myBtn" class="ui-link">
<span class="number"> </span>
<span class="button "> </span>
</a>
I want dynamically to insert some content into <span class="number">
var content = 99;
$('number').html(content);
but nothing change.
So what I need to do insert value into span that resulted node looks like this
<span class="number">99</span>
You are not using . to get class selector. Use $('.number') instead of $('number')
$('.number').html(content);
OR
$('.number').text(content);
insert text using class
var content = 99;
$('.number').html(content);
Try this:
$('.number').html(content);
You are referring to a wrong element. You can also do this:
$('.number').text(content);

Define javascript parameter in PHP [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
During a while loop in PHP I want to create following link:
echo '<a onclick="getSolution('.ResultArray['qid'].')" style="color: red;">Lösung anzeigen</a>';
But there it is a mistake. It will not take the $ResultArray['qid'] as a parameter for the javascript function. I need this parameter for define which div it has to take when clicking on the link.
Here you get the complete while loop in php:
while($ResultArray = mysqli_fetch_array($getQuestions)) {
echo '<p>';
echo $ResultArray['question'];
echo '<br />';
echo '<input type="text" style="width: 500px;"/>';
echo '</p>';
echo '<a onclick="getSolution('.ResultArray['qid'].')" style="color: red;">Lösung anzeigen</a>';
echo '<div id="'.$ResultArray['qid'].'" style="visibility: hidden">';
echo $ResultArray['answer'];
echo '</div>';
}
How I can fix this problem?
You're trying to reference your ResultArray variable without prefacing it with your $ symbol.
Corrected:
echo '<a onclick="getSolution('.$ResultArray['qid'].')" style="color: red;">Lösung anzeigen</a>';
echo '<a onclick="getSolution('.ResultArray['qid'].')"
^^^^^^^^^^^^---undefined constant, since it has no "$"
You must have $ResultArray in there. Without the $, it's a constant, and you cannot have constant arrays.

Categories