i have a function that creates an album and with jquery's post function and then it create the necessary HTML elements and append them to the document. the problem is that the elements are a link and its target, while the link woks fine when i refresh the page it seems like the browser couldn't found the added target which was created with jquery. how could i target an element with javascript that was created with either jquery or javascript. thanx in advance. EDIT:
code:
btn = document.getElementById('add_album_btn');
btn.addEventListener('click', add_album_btn_func, false);
function add_album_btn_func(){
div = $('<div>').addClass('albums_div');
a = $('<a>').addClass('albums');
a.attr('onclick', 'return false;');
a.attr('onmousedown', 'autoScrollTo("new_section");').attr('href', '#');
a.append('★ New Album');
div.append(a);
section = $('<section>').attr('id', 'new_section');
header = $('<header>').addClass('inner_header');
header.append($('<h4>').append('New Album'));
inner_section = $('<section>').append($('<h5>').append('Images List :'));
footer = $('<footer>').addClass('inner_footer');
upload_btn = $('<a>').attr('id', 'new_section').addClass('upload_file ajax');
upload_btn.attr('href', 'some/link/');
upload_btn.append('Upload a file');
footer.append(upload_btn);
section.append(header);
header.after(inner_section);
inner_section.after(footer);
$('#wrap').append(div);
$('#separator').after(section);
}
var scrollY = 0;
var distance = 40;
var speed = 24;
function autoScrollTo(el){
var currentY = window.pageYOffset;
var targetY = document.getElementById(el).offsetTop;
var bodyHeight = document.body.offsetHeight;
var yPos = currentY + window.innerHeight;
var animator = setTimeout('autoScrollTo(\'' + el + '\')', speed);
if(yPos >= bodyHeight){
clearTimeout(animator);
}else{
if(currentY < targetY - distance){
scrollY = currentY + distance;
window.scroll(0, scrollY);
}else{
clearTimeout(animator);
}
}
}
EDIT: apparently i wasn't clear enough what i want is the following: i have this javascript code:
$(document).ready(function(){
$('.lightbox').click(function(){
$('.backdrop').animate({'opacity': '.50'}, 300, 'linear');
$('.box').animate({'opacity': '1.00'}, 300, 'linear');
$('.backdrop, .box').css('display', 'block');
});
$('.close').click(function(){
close_box();
});
$(".backdrop").click(function(){
close_box();
});
function close_box(){
$('.backdrop, .box').animate({'opacity': '.0'}, 300, 'linear', function(){
$('.backdrop, .box').css('display', 'none');
});
}
a = $('<a>').addClass('lightbox').attr('href', '#').append('<br>Click to Test.');
$('body').append(a);
});
the html:
<h1>jquery light-box</h1>
<a href=# class=lightbox>open lightbox</a>
<div class="backdrop"></div>
<div class="box"><div class="close">X</div>this is the light box</div>
the CSS:
body{
font-family: Helvetica, Arial;
}
.backdrop{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #000000;
opacity: .0;
z-index: 50;
display: none;
}
.box{
position: absolute;
top: 20%;
left: 30%;
width: 500px;
height: 300px;
background: #ffffff;
z-index: 51;
padding: 10px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
-khtml-border-radius: 10px;
border-radius: 10px;
box-shadow: 0 0 5px #444444;
display: none;
}
.close{
float: right;
margin-right: 6px;
cursor: pointer;
}
the problem when the second link is added with javascript it seems to be llike its invisible to the function. any help thanx in advance.
NOTE: this lightbox code is from the PHPacademy tutorials.
NOTE: if you'r going to give the answer about re_writing the function and re-assign it to the new created element i already know that i need another way.
thanx in advance.
href=#idOfElementAddedDynamically"links and target elements...