0

Regards. I am generating an .click event for every element of my table using a php loop like this:

<?php foreach ($elements as $element){ ?>
    var element_id = <?= json_encode($element['element_id']));?>;

    $('#'+element_id+'_button').click(function() {
        $('#'+element_id+'_interval').toggle();
        if ($(this).hasClass('active')){
            $(this).removeClass('active');
        }
        else{
            $(this).addClass('active');
        }
    });
    <?php }?>

the problem is, when $('#'+element_id+'_interval').toggle(); the element is not responding but it adds and removes the classes correctly, somebody help me please.

12
  • 2
    Woooo dude!! Mixing php with Javascript? That's new!! :D You can NOT do that! Commented Mar 15, 2013 at 17:11
  • 4
    @Steve of course you can. Commented Mar 15, 2013 at 17:12
  • PHP really has nothing to do with this. Post the rendered HTML please. Commented Mar 15, 2013 at 17:13
  • Not in the way he is trying to so so, unless he is trying to generate a .JS file on the fly... Commented Mar 15, 2013 at 17:13
  • 1
    @steve, yes you can use php to generate a html page which comtains the javascript that will be run once rendered. I see your confusion though. :) I use this method for complex google maps javascript. Commented Mar 15, 2013 at 17:15

3 Answers 3

3

Use a class, makes your code a lot cleaner, however if you have to do it this way you should know that your element_id variable is constantly being rewritten.

Try something like:

<button class="toggler" data-target="#interval_1"></button>

$(".toggler").click(function (ev) {
    ev.preventDefault();
    var target = $(this).data("target"); // would return #interval_1
    $(target).toggle();

    if ($(this).hasClass('active')) {
        $(this).removeClass('active');
    } else {
        $(this).addClass('active');
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

I would advise you to firstly separate your php from your javascript.
Otherwise (between other) it will be difficult to maintain.

As a start, you could do the loop with javascript

2 Comments

I have 352 elements so far and is going to get bigger, I need to do this with a php loop.
Actually you should not need to loop at all, look at @cernunnos answer
0

Put the binding events into a seperate method, and call the method in your document.ready script:

 $(document).ready(function() {
      bindBtns();
});

function bindBtns()
{
    <?php foreach ($elements as $element){ ?>
    var element_id = <?= json_encode($element['element_id']));?>;

    $('#'+element_id+'_button').click(function() {
        $('#'+element_id+'_interval').toggle();
        if ($(this).hasClass('active')){
            $(this).removeClass('active');
        }
        else{
            $(this).addClass('active');
        }
     });
     <?php }?>
}

2 Comments

I have my loop in .ready() is working fine, .click executes for every element separately but is not toggling if that is a word
Can you post the html output of 2-3 buttons including the objects you are looking to toggle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.