Hi Guys,
pretty easy question.
I haven’t used jquery for quite a while and I’m trying to simply alter out the text in a div with an ID of “test” but I’m running into trouble.
var mytest = $("#test");
alert(mytest);
I’m sure it’s because I’ve selected the div as a whole, but I’ve tried selecting the first child with no success.
I suppose I should first chain in a “p” selector, then select the first item, but I’m not quite sure how to do it.
When you use json.menu.popup.menuitem to access the array, the first item is at index 0, and the 3rd item is at index 2. So to get the value of the third item, resulting in “close”, you would use json.menu.popup.menuitem[2].value
sorry to re-post in a topic I just considered closed, but I’ve just come across a similar issue that I’d like some help with.
I’m playing with $getJSON and can select items where there’s 1 matching item, but am having trouble selecting a specific 1 when there are multiple matching items.
Below is my $getJSON jquery code:
$.getJSON("myExampleScripts/JSONExample1.js", function (json) {
alert("JSON Data: " + json.menu.popup.menuitem.value);
})
If I want to alert out the menu’s id or value, I have no problems, but if I want to alert out the value item of the 3rd menu item within “popup”, then I run into trouble.
I’m not sure how to select specific ones, forgive me, JSON is quite new to me.
If it fails, is there no way to specify a function to handle it? (Should I use $.ajax instead if I want to specify error functions?)
One of the benefits of doing it the second way is that you can save $(“#test p”) to a variable, to retrieve different parts of it later. If it’s just a once-only things though then the first way can be more effective.
I want to get the contents of the p tag in the test div.
If we assume it’s the first p tag in test how would I go about it.
Edit
After a quick look at the jquery docs, I can see that I just need to use:
$(“#test p”).html()
to get the first item, but what if I want the 3rd (just to help me understand it better)
$(“#test p”) will select all paragraphs in the test div, but if you’re just wanting to change the contents of the test div, you can use jQuery’s html method: