1

I have created nestled arrays, which I then append to a div. When i click the button with id "name", a movie with title is stored in an array $titelBetyg, which is later stored in another array $films. Whenever i create a new $titelBetyg, i want to remove the previous $films from my div, before replacing it with the new one. How do I do this?

Javascript

$(document).ready(function(){

var $films = [];

$('#name').keyup(function(){
            $('#name').css('background-color', 'white');
});

$('#options').change(function(){
            $('#options').css('background-color', 'white');
});

$("#button").click(function(){
    var $titelBetyg = [];

    var $titel = $('#name').val();
    var $betyg = $('#options').val();

    if($titel == ""){
        $('#name').css('background-color', 'red');
        alert("Fail");
    }
    else if($betyg == "0"){
        $('#options').css('background-color', 'red');
        alert("Fail");
    }
    else{

        $titelBetyg.push($titel);
        $titelBetyg.push($betyg);
        $films.push($titelBetyg);

        // here is where i need to remove it before appending the new one

        $('#rightbar').append("<ul>");
        for(i=0; i<$films.length; i++){
            $('#rightbar').append("<li>" + $films[i][0] + " " + $films[i][1] + "</li>" + "<br>");
        }
        $('#rightbar').append("</ul>");
    }
});

$('#stigande').click(function(a,b){

});
$('#fallande').click(function(){

});

});
3
  • why did youtag java ? java != javascript Commented Feb 27, 2013 at 14:48
  • 1
    Btw, there is no need in JS to prefix all variable names with $ Commented Feb 27, 2013 at 14:50
  • i know, it is just a habbit when using jquery Commented Feb 27, 2013 at 15:12

3 Answers 3

1

Use .empty() like this (and append to the <ul> instead of something else):

var $ul = $("<ul>");
for (var i=0; i<$films.length; i++) {
    $ul.append("<li>" + $films[i][0] + " " + $films[i][1] + "</li><br>");
}
$('#rightbar').empty().append($ul);

Btw, it might be easier to only append the new one instead of emptying and rebuilding the whole thing:

$('#rightbar ul').append("<li>" + $titel + " " + $betyg + "</li><br>");

To remove only the list contents (and nothing else) from the #rightbar, you could use this:

var $ul = $('#rightbar ul').empty();
if (!$ul.length) // if nonexistent…
    $ul = $("<ul>").appendTo('#rightbar'); // create new one


for (var i=0; i<$films.length; i++)
    $ul.append("<li>" + $films[i][0] + " " + $films[i][1] + "</li>");
Sign up to request clarification or add additional context in comments.

1 Comment

the problem with that solution is that it removes everything in my div. I have a h2 " Filmer" which also gets removed.
0
document.getElementById('rightbar').innerHTML = '';

That way rightbar is totally empty.

3 Comments

This is not recommended if you use jQuery.
It is much faster though than using jquery. When using jquery you are not bound to using jquery exclusively.
Yeah, but emptying with jQuery has the sideeffect of removing custom data and cleaning event listeners and co from the removed parts to prevent memory leaks. While of course not necessary everywhere, it still is advisable.
0

You only require to remove the content of the container. So, use the .empty() function

$('#rightbar').empty().append("<ul>"); //It will empty the content and then append
for(i=0; i<$films.length; i++){
    $('#rightbar').append("<li>" + $films[i][0] + " " + $films[i][1] + "</li>" + "<br>");
}
$('#rightbar').append("</ul>");

1 Comment

the point is to replace the array. I dont want to remove the whole content, since i have an h2"Filmer" in there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.