0

I'm using a script where I need to make multiple events to make a popup appear.

I tried this, but it doesnt work:

for (i=0;i<=storingen;i++)
    {
        $("#storing" + i).click(function(){ centerPopup(); loadPopup(); });
    }

The output should be:

$("#storing0").click(function(){ centerPopup(); loadPopup(); });
$("#storing1").click(function(){ centerPopup(); loadPopup(); });
$("#storing2").click(function(){ centerPopup(); loadPopup(); });
$("#storing3").click(function(){ centerPopup(); loadPopup(); });

etc.

But the amount of divs with the id #storing(number here) is variable, so i wanted to make this, but it doesnt work...

I get the storingen variable from php:

<script type="text/javascript">aantalstoringen('.$aantalstoringen.')</script>

which i pick up in the js file like this:

function aantalstoringen(storingen){
    storingen=storingen;
}

I did an alert(storingen), which traced the right number, so that is ok.

COuld it be that the for loop doesnt work because that isnt in the aantalstoringen function, but in another function:

$(document).ready(function() {

I used this tutorial to make the javascript: http://yensdesign.com/2008/09/how-to-create-a-stunning-and-smooth-popup-using-jquery/#popup1 and the script you get is this: http://yensdesign.com/tutorials/popupjquery/popup.js

2
  • I suggest you give all these elements the same class and just do $('.classname').click(function(){...});. Commented Oct 16, 2011 at 15:44
  • Also, I'd like to point out that this function is made [like you can see in the linked popup.js file] to show specific divs. Each div has a different ID because the content is different, so i cant use that function that searches for every element with ID storing* I'm sorry if I'm not very clear. Commented Oct 16, 2011 at 16:55

5 Answers 5

1

Use the [name^="value"] selector instead:

$('[id^="storing"]').click(function(){ ... });

Basically, it's saying "find all elements whose ID begins with 'storing'."

If you need it more explicit, you can test the id inside a each() to apply better filtering. e.g.

$('[id^="storing"]')
  // make sure all IDs end in a number
  .each(function(i,e){
    if (/\d$/.test(e.id)) {
      // now that we only have ids that begin with storing and end in
      // a number, bind the click event
      $(e).click(function(e){ ... });
    }
  });
Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure what you want to achieve with the mapping... you will end up with an array of strings and you cannot bind event handlers to them.
@FelixKling: True, still waking up. Should probably put the logic in the click statement or an each statement
In that case I would use .filter instead ;) No worries, good morning :)
0

You don't create dozens of event listeners that call the same handler. You create one listener on a higher level in the DOM and make it react only if the ID of the target matches the pattern.

This is why libs like jQuery are teaching kids bad manners... -.-

1 Comment

Yeah, you know, I realised that the script I used is totally not handy to use here, as they all call the same handler... I have to make different eventlisteners which call different handlers to make this work... how stupid of me..
0

It could be any number of things. It would help if you showed us more of the code, like all of the aantalstoringen function.

The following should work:

function aantalstoringen(storingen) {
    $(document).ready(function() {
        for (i=0;i<=storingen;i++) {
            $("#storing" + i).click(function(){ centerPopup(); loadPopup(); });
        }
    });
}

That said, this is a really bad way to do this. I would have each of your elements also include class="storing". Then you don't have to get the number of objects from the server:

$(document).ready(function() {
    $(".storing").click(function(){ centerPopup(); loadPopup(); });
});

1 Comment

I showed you all of the aantalstoringen function, its just that line to import the variable which is traced by PHP.
0

First you give a class name to the div like class="popupDiv". Then you try something like this

$('.popupDiv').live('click', function(){
      // var id = $(this).attr('id'); By doing so you will get the current id of the div too.
      centerPopup();
      loadPopup(); 
});

Comments

0

As Brad correctly said, you don't have to know the amount of such elements, you can just iterate over all element having id starting with something.

So your code would be:

$(document).ready(function() {
    $('[id^="storing"]').click(function() {
        centerPopup();
        loadPopup();
    });
});

Comments

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.