2

i'm learning JavaScript and have this assignment where I must build a table where i enter content. Every row must get an index number. And when this works you must be able to input the row number and delete this.

So far, so good.

BUT when i inputted multible rows and deleted the 3rd of 5 rows. And i saw that the row index numbers go from 1, 2, 4, 5. So it doesnt update. Now i want to fix that.. but how?!

I cant think of a way to only call uppon the 1st number of every row.. I hope you can help me..

The link to my project(sorry in advance, i am dutch!): https://jsfiddle.net/Burner/kaqsgra1/

My loops for inputting the rownumber:

function addContent(){
    for(i = 0; i < f.length; i++){
        newData[i] = f.elements[i].value;
    }

    var newRow = contenttable.insertRow();

    for (var i = 0; i< 4; i++){
        newData[0] = contenttable.rows.length - 1;
        var newCel = newRow .insertCell(i);
        newCel.innerHTML = newData[i];
    }    

   }

And when deleting a number i call this function:

function deleteNumber(){
    var numRows = contenttable.rows.length - 1;

    if(deleteNr.value == ""){
        alert("Voer svp een nummer in!");
        return false;
    }
    if(deleteNr.value > numRows){
        alert("Dit rijnummer bestaat niet ");
        return false;
    }
    document.getElementById("uitvoertabel").deleteRow(deleteNr.value);
}
3
  • could you update your code snippet to english? Commented Jan 17, 2017 at 13:31
  • @DomeTune, did it! Commented Jan 17, 2017 at 13:38
  • have a look at this fiddle jsfiddle.net/aakashmartand/246grrrw Commented Jan 17, 2017 at 14:10

1 Answer 1

2

In your deleteNumber function, renumber your rows after the selected row is deleted.

for (var i = 0; i< uitvoertabel.rows.length; i++){
  uitvoertabel.rows[i].cells[0].innerHTML = i;
} 

JSFiddle

Sign up to request clarification or add additional context in comments.

1 Comment

I like you! Thx!!

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.