Here is my jQuery:
jQuery( document ).ready( function ( $ ) {
$('ul li:first-child').addClass( 'first_item' );
var className = $('.first_item').attr('id');
alert('Date is:'+ className +'.');
});
And then this another section of jQuery, that im trying to contrust a variable in:
j(".refreshMe").everyTime(5000, function (i) {
j.ajax({
url: "test.php?latest=" + className + "",
cache: false,
success: function (html) {
j(".refreshMe").html(html);
}
Its this line:
url: "test.php?latest="+className+"",
Im trying to take the className variable and feed it into this line.
Can anyone suggest what im doing wrong here?
Looks to me that the problem is simply the fact that you declared “className” inside that “ready” handler, so it’s not visible to the other code.
Pull the declaration out of there and make it global (though see below):
Then remove
varon the line in the “ready” handlerNow, global variables like that certainly work, but it’s kind-of ugly. It might be nicer if you just moved that “everyTime” thing inside the same “ready” handler and keep the variable local.