Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
MonkeyMaker
May 22, 2006

What's your poison, sir?
OK, I have a JSON object with two sub-objects. How do I reference a sub-object by name, not index?

Here's the JSON:
code:
{"menus":
	[
		{"buckets":
			[
				{"name":"item name", "price":"9.99", "description":"item description"},
				{"name":"item name", "price":"9.99", "description":"item description", "subs":
					[
						{"name":"item name", "price":"9.99"}
					]
				},
				{"name":"item name", "price":"9.99", "description":"item description"}
			]
		},
		{"baskets":
			[
				{"name":"item name", "price":"9.99", "description":"item description2"},
				{"name":"item name", "price":"9.99", "description":"item description2", "subs":
					[
						{"name":"item name", "price":"9.99"}
					]
				},
				{"name":"item name", "price":"9.99", "description":"item description2"}
			]
		}
	]
}
which validates as...valid...on jsonlint.com. I can access ['menus'] or ['menus'][0], but trying to get ['menus']['buckets'] fails. I have to be able to do this by name since it'll be client-updated as plain text.

Adbot
ADBOT LOVES YOU

MonkeyMaker
May 22, 2006

What's your poison, sir?
Thanks to both of you. Removing that level of nesting and the brackets fixed it.

MonkeyMaker
May 22, 2006

What's your poison, sir?

uG posted:

I'm getting the html for the form from an ajax request, so it doesn't exist until after $(document).ready. How is the javascript that loads with the original page supposed to know when my new form suddenly appears? Do I need to tie into the javascript that makes the ajax get call that gets the form html?

I'm assuming that is the problem, as the code you posted doesn't seem to be catching the submit either. It's my first day with javascript, so forgive me if I am missing something you already said.

code:
$("#myFormId").live('submit', function() {
    // whatever
});
That'll execute on submit of any form with that ID. Obviously, to be really useful, you want classes instead of IDs.

MonkeyMaker
May 22, 2006

What's your poison, sir?

ZippySLC posted:

I have a bit of Javascript on my website that grabs a random image and uses it for a banner:

code:
javascript...
Can someone suggest me a way that I can just have it look at whatever images are in the header directory and use one, rather than explicitly specifying filenames in the code?

I should say that I am not a Javascript programmer and that this is someone elses code example that I used and have outgrown.

Javascript can't access the filesystem (as best I know). You could have some sort of PHP/etc script that provides a list of files, grab that through AJAX and then select an image from that list.

MonkeyMaker
May 22, 2006

What's your poison, sir?

Leshy posted:

I've checked a few pages back, but couldn't find this issue or similar.

I have a page listing multiple contacts at various companies. They're organised in a list, with each company rendered as a 'block' that can be opened or closed with a link through a simple javascript function.

HTML5's summary/details markup plus the polyfill would be my solution.

MonkeyMaker
May 22, 2006

What's your poison, sir?

Raskolnikov2089 posted:

JavaScript code:
      function arrayCounter(x){
        if (typeof x === 'string' || 'number' || 'undefined')
	{
        	return 0;
        }
        else
	{
          return x.length;
        }
      };
The above only works if I give each || statement it's own typeof operator. i.e.


JavaScript code:
      function arrayCounter(x){
        if (typeof x === 'string' || 
	    typeof x === 'number' || 
	    typeof x === 'undefined')
	{
        	return 0;
        }
        else
	{
          return x.length;
        }
      };
Is this a particular quirk of typeof, or am I just assuming javascript is a lot more intuitive than it actually is?



vvv That makes sense (and is kind of fascinating). thank you.

JavaScript code:
switch (typeof(x)) {
    case 'string':
    case 'number':
    case 'undefined':
        return 0;
        break;
    default:
        return x.length;
        break;
}
yes?

Adbot
ADBOT LOVES YOU

MonkeyMaker
May 22, 2006

What's your poison, sir?

Raskolnikov2089 posted:

So best practices wise, am I doing myself a disservice by leaving really detailed, obvious notes in my code? As in making myself look bad?

For instance, I explain why each div is hidden and under which circumstances it will be revealed, which a more experienced coder could no doubt easily figure out by reading my code.

Strip the comments from whatever compiled/deployed version your code ends up in, but, no, you're doing the right thing. There is never enough documentation.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply