Forum Moderators: open

Message Too Old, No Replies

onclick="clear.", javascript not being called

         

Br3nn4n

1:05 pm on Apr 3, 2010 (gmt 0)

10+ Year Member



Got a little code here:


function clear(fields) {
elms=fields.split(',');
for(i=0;i<elms.length;i++) {
$(elms[i]).innerHTML = '';
}


it's to clear the output of a calculator I'm working on. Used in conjunction with this.form.reset() I can clear the entire "calculator table". It basically needs to empty a couple table cells which would be filled with the calculator's output.

So, I'm calling it like this:

<input type="button" ....... onclick="clear('pipm,prpm')" />

The argument in the single quotes is the ids of the elements I want to empty, the table cells that is. Here's the weird thing - it works when called from the URL line or JS console, but NOT when the "Clear" button is clicked.

Why isn't this working? Been bugging me all night. The rest of my functions work like a charm but not this guy.

Thanks again!

Br3nn4n

10:12 pm on Apr 3, 2010 (gmt 0)

10+ Year Member



No ideas? I've tried pretty much everything I can think of..

lavazza

2:55 am on Apr 5, 2010 (gmt 0)

10+ Year Member



>> onclick="clear('pipm,prpm')" />

>> The argument in the single quotes is the ids of the elements

I think that the argument in the single quotes is a 9-character string

Instead of
<input type="button" ....... onclick="clear('pipm,prpm')" />
Try
<input type="button" ....... onclick="clear(pipm,prpm)" />

Fotiman

1:35 pm on Apr 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your code looks like it should work to me. The only things I notice are:

1) Add a semicolon:
<input type="button" onclick="clear('pipm,prpm');" />

2) Use the var keyword to declare your elms array and i variable, otherwise you'll be writing to global variables:

function clear(fields) {
var elms = fields.split(',');
var i;
for (i = 0; i < elms.length; i++) {
$(elms[i]).innerHTML = '';
}
}


3) Your post does not include the closing } to end the function. Is that just a copy/paste error, or is that missing from your code?

daveVk

3:28 am on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$(elms[ i ]).innerHTML = '';

gives

$('pipm').innerHTML = '';

assuming $ refers to JQuery ?

you need

$('#pipm').html('');