Re: php generated html needs simple javascript function, yet my javascript sux



Randy Webb wrote:
mk said the following on 7/27/2006 4:46 PM:

Greetings all - im new to this newsgroup so pls dont flame me :p


It is not a person's newness to this group that gets them flamed, it is the behavior and/or expectations of people that get them flamed.

I need some help! Please view the html below in a browser. Or goto this url -> http://firebrain.co.uk/java-problem.htm


It's not a Java-problem but ok.

(Assuming you have seen the rendered code)
Basically I need the X buttons to change all the values of each textbox to zero either in the col or row depending on which button is clicked.


Ok, loop through the form, check for the indexOf your identifying string.

Function summery:
If you click on a X button on the right hand side of the table it resets all the textboxes in that row. And if you click one of the X buttons along the base of the table it will reset all the textfields in the column.

I hope the way I named my fields can be for>looped in javascript :)


All form elements can be for>looped, irrelevant of the names. Your naming convention make it simpler, somewhat, but the naming convention could be better. column1row1 and so on, so that the name tells you the column and the row it is in.

If you have not seen the HTML in a browser yet please do so because im sure there is a very easy solution to my problem.


There is, I gave it above. Search the archives on how to do it.

Please please please - this is for a client and im running low on time


Is your client going to pay me for writing the code for the project?

Any help would be much appreciated! Any questions just ask
Please reply to this message or (preferably) send me an email to
javascript@xxxxxxxxxxxxxxx


Don't ask, and definitely don't expect, an email reply. You ask in Usenet, you get answered in Usenet.

P.S. You only need one function, not two, to do what you want to do.

Here is you a start:

function resetColOrRow(identifier,formRef){
totalElements = formRef.length;
for (i=0;i<totalElements;i++){
if (formRef.elements[i].name.indexOf(identifier) != -1)
formRef.elements[i].value = 0;
}
}

And call it as such:
resetColOrRow('[1]',this.form)
resetColOrRow('[2006-07-29]',this.form)

Where the first parameter to the function call is the unique part of the element name you want to match.

It can be made more efficient, I just don't feel like it right now. It should be tested as well.

There is a bug in that code, related to your HTML as much as to my code, I will leave that as an exercise for you to discover.


excellent "starter" function - it turned out to do the entire job very nicely indeed :) you were correct about the html bug(s) - i found there were 2 - first was the fact i gave the buttons a name in the same format as the textboxes (therefore the button would change to 0 also lol) and secondly my naming convention for the bottom row of buttons changed the col values to the left (i.e. -1) of the reference

everything fixed now and working perfectly here is the final code if anyone is interested:

<script language="JavaScript">
function resetColOrRow(identifier,formRef){
totalElements = formRef.length;
newVal=prompt("Please enter new value for entire row/col","0");
if(!newVal.match(/^\d+$/)){
alert("Was not a number");
}else if(newVal.length>2){
alert("Number was too long");
}else{
for (i=0;i<totalElements;i++){
if (formRef.elements[i].name.indexOf(identifier) != -1)
formRef.elements[i].value = newVal;
}
}
}
</script>

Thanks again :)

.