0

I am calling a jquery function from .aspx page as follows

 <body onload="JavaScript:createPanels('[{a,b,c,d,e}]')">

in my jquery I have function defined as

function createPanels(requiredButtons) {
    var abc = JSON.stringify(requiredButtons)
    abc.each(function (key, value) {
        alert(value);
    });

}

Now the issue is that I am not getting any alert.

Can any one point me where am I going wrong?

2
  • 3
    If you want to call each like that it'd have to be on a jquery object, so $(abc).each. Otherwise you would call $.each(abc, ...). It's in the docs. Commented Jul 23, 2013 at 7:38
  • if you're looking for the javascript array iterator, you need to use .forEach Commented Jul 23, 2013 at 7:44

6 Answers 6

1

Thats not how $.each is to be used:

Try this:

$.each(abc, function (key, value) {
    alert(value);
});

It will alert each character in the string.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried this but now its giving me alert for each single character in "{a,b,c,d}" including quotes and commas and braces. I only want alert for a b c and d
1

This is not a valid JSON format. Json Format is like as below:

 var obj = {
     "flammable": "inflammable",
      "duh": "no duh"
   };

Then Use as below:

 $.each( obj, function( key, value ) {
     alert( key + ": " + value );
  });

Reference

Comments

1

Several issues there.

a. This is not JSON [{a,b,c,d,e}]

If you really want to do it on the body element then this is how to do it:

<body onload="JavaScript:createPanels('{&#34;posts&#34;: [{&#34;key&#34;:&#34;value&#34;}, {&#34;key&#34;:&#34;value&#34;}]}')">

This is not a really good idea, so you call it in a jquery page load handler:

$(document).ready(function(){
  //create the JSON object here
  //call the function here
}); 

b. Change your script too:

function createPanels(requiredButtons){
    var abc = JSON.parse(requiredButtons);
    $(abc).each(function (key, value) {
         alert(value);
    });
}

Comments

0

"{a,b,c,d}" is not a object that's why your code is not working.

Second point is : Use jQuery.each() for iterating a collection.

Try to put it in this form :

var x= ['a','b','c','d','e'];
jQuery.each(x,function (key, value) {
        console.log(value);
});

It will return a,b,c,d,e as you want.

Here is the working demo

Comments

0

I lurve jQuery and everything, but there's no need to invoke it to iterate an array.

abc.forEach( function (elem) {
    console.log(elem);
});

should work, once you've tidied up your JSON thing

Comments

0

Thanks for your valuable suggessions I solved the problem as

.aspx page

<body onload="JavaScript:createPanels('a,b,c,d,e')">

jquery

function createPanels(requiredButtons) {
    var abc = requiredButtons.split(',');
    $.each(abc, function (key, value) {
        alert(value);
    });

}

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.