0

I need some help with this jQuery code. The code below works fine but i tried to use $.each instead and its not working.

$(document).ready( function(){

    $('#pull').click( function(event){
        event.stopPropagation();
        $('#pf').slideToggle();
    });
     $('#pullo').click( function(event){
        event.stopPropagation();
        $('#pt').slideToggle();
    });
      $('#pullc').click( function(event){
        event.stopPropagation();
        $('#pc').slideToggle();
    });

    $(document).click( function(){
        $('#pt').hide();
        $('#pf').hide();
        $('#pc').hide();
    });
});

The code below is not working for me, please help

var pul = ["#pull":"#pt","#pullo":"#pf", "#pullc":"#pc"];

$(document).ready( function(){

  $.each(pul, function(i, v){
    $(i).click( function(event){
        event.stopPropagation();
        $(v).slideToggle();
    });
    $(document).click(function(){
        $(v).hide();

    });
  });  


});
5
  • Looks like your array syntax is invalid. You should curly braces for objects ("associative" arrays). Commented Jan 16, 2014 at 19:42
  • 2
    Your pul array should be an object, that's invalid JavaScript. It seems you want to refactor the script. Can you post the HTML? Commented Jan 16, 2014 at 19:42
  • It also looks like you mixed up #pt and #pf. Commented Jan 16, 2014 at 19:45
  • Just a note, when you click on the document all of your elements will .hide(). Commented Jan 16, 2014 at 19:51
  • Thank you , it's working now. Syntax error should have used {} instead of [] :) Commented Jan 16, 2014 at 20:02

3 Answers 3

1

Make your collection object instead of array:

Here is Demo

var pul = {"#pull":"#pt","#pullo":"#pf", "#pullc":"#pc"};

$(document).ready( function(){

  $.each(pul, function(i, v){
    $(i).click( function(event){
        event.stopPropagation();
        $(v).slideToggle();
    });
    $(document).click(function(){
        $(v).hide();    
    });
  });  
Sign up to request clarification or add additional context in comments.

1 Comment

$(''+i) can just be $(i). No need to "convert" it to a string.
0

Try this,

var pul = {"#pull":"#pt","#pullo":"#pf", "#pullc":"#pc"};

$(document).ready( function(){

  $.each(pul, function(i, v){
    $(i).click( function(event){
        event.stopPropagation();
        $(v).slideToggle();
    });
    $(document).click(function(){
        $(v).hide();

    });
  }); 
});

1 Comment

Thank you , it's working now. Syntax error should have used {} instead of [] :)
0

You have two ways you can solve this. Either make your array as 2D array:

var pul = [
    ["#pull", "#pt"],
    ["#pullo", "#pf"], 
    ["#pullc", "#pc"]];

And you can do $(v[0]).click(... instead of $(i).click(.... And $(v[1]).hide(); instead of $(v).hide();.

Or, you can convert it to an object like others have suggested:

var pul = {"#pull":"#pt","#pullo":"#pf", "#pullc":"#pc"};

Right now, you have an invalid object and an invalid array.

1 Comment

I converted it to a valid object and its working now. Thank you

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.