SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array +JSON +javascript

    how r u

    i need help in JSon array
    Textt(Temp_J) is a function which sends items but Json arry doesn't accept it

    can u help plz

    i have this code

    PHP Code:

    var J={ "myMenu" : [   
    "title"" ",  
    "popMenu":[
              {
    "title":" ""popMenu":[
                            {
    "title":" "}
                             ]
    }
              ]
    },

    ]};



    function 
    creat_J() // to add random items to array in JSON 


      
    Temp_J =randomFromTo(min_lengthJmax_lengthJ);//call random function to send a number 

    for(var b=0;b<7;b++){ 

    J.myMenu[b]={"title":Textt(Temp_J) , "popMenu":[{"title":"tt""popMenu":[{"title":"tttt"}]}]}; 

    // Textt(Temp_J) is function which send random text from other array  
    alert(J.myMenu[b].title); 


  2. #2
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Can you show us the source of the "Textt()" function?

    Have you tried seeing if Textt(Temp_J) actually returns a value before you try to insert it in to the JSON object?
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  3. #3
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    immmm completing to my first q ,, i supposed have these :

    PHP Code:
    var text4 = ["File""Edit""Help","Save""View""Find"];
    var 
    text5 = ["Print""Close""Paste","HEART","HEARS","SEARS","STARS","STARE","STORE","STONE"]; 
    and our function is :
    PHP Code:
    function Textt(num)
    {

    //alert(num);

       
    switch(num)
        {
        case 
    4:
         
    document.write(text4[r]); 
          break;
        case 
    5:
        
    document.write(text5[r]); 
         break;
        default:
        
    document.write("Item!");
     
        } 
    i want that these above array add to the Json Arry

  4. #4
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    You're going to want to return a value from the Textt() function rather than using document.write

    e.g.
    Code javascript:
    function Textt(num) {
      switch(num)  {
        case 4:
          return text4[r]; 
        case 5:
          return text5[r]; 
        default:
          return "Item!"
      }  
    }

    Additionally, where is the variable r being set? You'll need to make sure that it's available in the Textt() function.
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  5. #5
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    r is counter
    when is called this function, it will increase 1

    function Textt(num)
    {
    r=r+1;
    }

    my problem is in this line :
    J.myMenu[b]={"title":Textt(Temp_J) , "popMenu":[{"title":Textt(Temp_J), "popMenu":[{"title":Textt(Temp_J)}]}]};

    how i can add item in Json array from other array

  6. #6
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    function Textt(num)
    {
    r=r+1;
    //alert(num);

    switch(num)
    {
    case 4:
    document.write(text4[r]);
    break;
    case 5:
    document.write(text5[r]);
    break;
    }

  7. #7
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    You'll need to return the values from Textt() like I showed you in my previous post. When you do that, Textt() will, instead of writing the value to the page, output it to the array.
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  8. #8
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you
    it works with me


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •