Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What's wrong with the following uncheck checkbox code?

18 views
Skip to first unread message

justaguy

unread,
Feb 23, 2018, 7:01:05 AM2/23/18
to

For the following FORM, if for whatever reason the user has already checked the checkbox field with id of "sig" but its prior field's value is invalid, uncheck this checkbox. See below. However, the uncheck function fails.
document.getElementById('sig').checked = false;
Its syntax seems correct. Why? Thanks.

<form>
enter promo code:
<input type="text" id="code" name="code" onblur="verify(this)">
<span id="note" style="color:red"></span>

yes to include signature: <input type="checkbox" id='sig' name='sig' value=1>

<!-- other elements below -->
...
<input type="submit" name='send'>
</form>

<script>
var verify = function {
if (this !== 'whatevercode') {
// alert the user
document.getElementById('note').innerHTML = 'Your promo code is invalid';

// uncheck signature checkbox field
document.getElementById('sig').checked = false;
}

}
</script>

Evertjan.

unread,
Feb 23, 2018, 7:22:24 AM2/23/18
to
justaguy <lichun...@gmail.com> wrote on 23 Feb 2018 in
comp.lang.javascript:

>
> For the following FORM, if for whatever reason the user has already
> checked the checkbox field with id of "sig" but its prior field's value
> is invalid, uncheck this checkbox. See below. However, the uncheck
> function fails. document.getElementById('sig').checked = false;
> Its syntax seems correct. Why? Thanks.
>
> <form>
> enter promo code:
> <input type="text" id="code" name="code" onblur="verify(this)">
> <span id="note" style="color:red"></span>
>
> yes to include signature: <input type="checkbox" id='sig' name='sig'
> value=1>

<input type="checkbox" id='sig' name='sig'>

There should better be no "value=1" in your input checkbox,
if you want it checked by default, use 'checked'.

> <!-- other elements below -->
> ...
> <input type="submit" name='send'>
> </form>
>
> <script>
> var verify = function {
> if (this !== 'whatevercode') {


in input type="text"
'this' cannot be a string by itself,
as it is/points to a DOM-object.
so you could use this.value,
and in the function,
you have to reference this 'this',
not write this 'this'.


onblur = "verify(this);"

var verify = function(t) {
if (t.value !== 'whatevercode')
...
};

or

'use strict';
const verify = (t) => {
if (t.value !== 'whatevercode')
...
};


> // alert the user
> document.getElementById('note').innerHTML = 'Your promo code is
> invalid';
>
> // uncheck signature checkbox field
> document.getElementById('sig').checked = false;
> }
>
> }
> </script>
>



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

justaguy

unread,
Feb 23, 2018, 8:45:17 AM2/23/18
to
Yeah, the "this" reference of the element's value was my oversight.

Excellent. Thank you.
0 new messages