Max Max - 1 month ago 13
jQuery Question

.off() click function of DIV

I have a list of

php/mysql
results where I added a function click to go to an url, like this

<li><div id=m_1>DATA</div> <button id=bu_1></button></li>
<li><div id=m_2>DATA</div> <button id=bu_2></button></li>
<li><div id=m_3>DATA</div> <button id=bu_3></button></li>
<li><div id=m_4>DATA</div> <button id=bu_4></button></li>
<li><div id=m_9>DATA</div> <button id=bu_9></button></li>
<li><div id=m_13>DATA</div> <button id=bu_13></button></li>


I need that anytime I press
button
id=bu_n
I deactivate the sending to the url of its respective
div
. click
div
id=m_n
go to url
page.php?id=n

I was trying with
on
and
off
.

$(this).on('click', function(){
window.location = "http://www.google.com/?id="+n;
});

$(this).off('click', function(){
window.location = null;
});


My problem is that the list generates dinamically the results, so is not predictable to recover the
id
of the
div
and using it in a function like this
function(id_result){}
.

Answer

You will need another selector, something like:

$('button').on
$('button').off

or add a class to each button and use it to select:

$('.btn').on
$('.btn').off

and then inside:

$('.btn').on('click', function(){
   window.location = "http://www.google.com/?id="+ $(".btn" ).index(this);
});

but I don't know what is window.location = null; needed to do exactly here

https://jsfiddle.net/amrelgarhy/mt46pc3s/