jQuery variable saved as JavaScript var unexpected behavior -
in script had following:
$('#image-slider').empty(); which emptied image slider element in application.
i wanted move directly using id references in function declared variables @ top of script including:
var globals = []; globals.markup = []; globals.values = []; globals.markup.image_slider = $('#image_slider'); however when call:
globals.markup.image_slider.empty(); the slider not emptied.
any ideas i'm doing wrong?
edit:
a full example:
$(document).ready(function(){ var projects = <?= $json; ?>; var globals = []; globals.markup = []; globals.values = []; globals.markup.title = $('#title'); globals.markup.image_slider = $('#image_slider'); function load_project(f) { var potential = window.location.hash.substring(1); $.each(projects, function(i, project){ if (project.permalink == potential) { // manage stats , fields $('#title').text(project.title); $('#agency').text(project.agency); $('#description').text(project.description); $('#website_url').attr('href', project.website_url); // manage images globals.markup.image_slider.empty(); ..... edit: idiot. simple typo. image_slider should have been image-slider .. bad.
it looks variable initialization script runs before element created, hence jquery collection that's assigned globals.markup.image_slider empty. need put code domready script:
$(function() { globals.markup.image_slider = $('#image_slider'); }); update: code sample, looks did that. try moving later - "load" event ( $(window).bind("load", ...); ) - or check logic ensure element rendered document , not dynamically created later in "load_project" somewhere.
Comments
Post a Comment