I’m getting a text from server side and I want to show one word for each button click. Here is my approach:
$(document).ready(function()
{
$.ajax(
{
type: "GET",
dataType: 'json',
url: "request.php",
success: function (response){
var words = text.match(/\b([a-z]{1,})\b/gi); //text is an element of json array
for (i = 0; i < 10; ++i)
$("#container").append("<span>"+words[i]+"</span>").hide();
}
});
});
My function to display words. It doesn’t work, just for explaining what I’m trying to do.
$(function()
{
$("#button").click(function(){
$("#container span").fadeIn(450); // shows nothing
});
});
Jquery selector can’t select the spans because they are not at html. Could you advise me a solution?
(When I wrote $(“#container”).fadeIn(450); it shows all the words.)
There two problems with your code;
You’re using
.append()to appendspanelements to#containerbut it returns#containerjQuery object. So everytime you append aspanelement you call.hide()for#container. As you can see I changed it a little. You have to createspanelement, hide it and then append it to#container, like this;Second problem is with your
.click()function. Your selector selects all span elements, there is pseudo selectors for selecting hidden elements or first element etc. Using them together you can select first hidden element like this;And finally implementing these to your code will give us this result;
But of course, if you want to
fadeInall of yourspanelements at once you can use your piece of code;Here is an example of your code.