/    Sign up×
Community /Pin to ProfileBookmark

help with to understand decrement

hi there

i have taken this script from internet
here i didn’t understand what does mean

[code=php]i–[/code]

in deleterow() function?

[code=php]<HTML>
<HEAD>
<TITLE> Add/Remove dynamic rows in HTML table </TITLE>
<SCRIPT language=”javascript”>
function addRow(tableID) {

var table = document.getElementById(tableID);

var rowCount = table.rows.length;
var row = table.insertRow(rowCount);

var cell1 = row.insertCell(0);
var element1 = document.createElement(“input”);
element1.type = “checkbox”;
cell1.appendChild(element1);

var cell2 = row.insertCell(1);
cell2.innerHTML = rowCount + 1;

var cell3 = row.insertCell(2);
var element2 = document.createElement(“input”);
element2.type = “text”;
cell3.appendChild(element2);

}

function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;

for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
table.deleteRow(i);
rowCount–;
i–;
}

}
}catch(e) {
alert(e);
}
}

</SCRIPT>
</HEAD>
<BODY>

<INPUT type=”button” value=”Add Row” onclick=”addRow(‘dataTable’)” />

<INPUT type=”button” value=”Delete Row” onclick=”deleteRow(‘dataTable’)” />

<TABLE id=”dataTable” width=”350px” border=”1″>
<TR>
<TD><INPUT type=”checkbox” name=”chk”/></TD>
<TD> 1 </TD>
<TD> <INPUT type=”text” /> </TD>
</TR>
</TABLE>

</BODY>
</HTML>
[/code]

thank you for attention

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@KorNov 26.2010 — The moment you remove an element from a collection, its structure and length changes, right? If you delete/remove the element with the index, say 3, from a collection with 6 elements, now the former [4] element becomes the [3] (in the place of the removed one), and the former [5] becomes [4] while the collection changes its length from 6 to 5. Right? If so, the initial conditions of the loop are not the same, which will bring errors, if we let it as it is.

To adopt the loop to the new structure, the code must decrease the indent with 1 unit. Same with the length of the collection. Otherwise the next loop will skip the next element, and in the end will give an error, as it will try to reach a much too high limit as length.

Well, to avoid that (which looks so intricate to you), the code could have been written using an [I]inverse loop[/I]
<i>
</i>for([COLOR="Blue"]var i=rowCount-1; i&gt;=0; i--[/COLOR]) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox &amp;&amp; true == chkbox.checked) {
table.deleteRow(i);

<i> </i> }

<i> </i> }

Now the length is not important, nor the index, because the loop is made backwards. In other words, the deleted element's index(position) "[I]remains behind[/I]", same as the affected (by the change) elements of the collection.
Copy linkTweet thisAlerts:
@azegurbauthorNov 26.2010 — thank you very much i understood. but i have one question too.

is there difference between?
[code=php]if(null != chkbox && true == chkbox.checked) {
table.deleteRow(i);

}
[/code]

and

i[code=php]f(chkbox != null && chkbox.checked == true ) {
table.deleteRow(i);

}[/code]


thanks you again
Copy linkTweet thisAlerts:
@KorNov 26.2010 — thank you very much i understood. but i have one question too.

is there difference between?
[code=php]if(null != chkbox && true == chkbox.checked) {
table.deleteRow(i);

}
[/code]

and

i[code=php]f(chkbox != null && chkbox.checked == true ) {
table.deleteRow(i);

}[/code]


thanks you again[/QUOTE]


No, there is no difference. The order of the terms has no relevance [I]within a comparison[/I].
Copy linkTweet thisAlerts:
@azegurbauthorNov 26.2010 — Thank you very much
×

Success!

Help @azegurb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...