Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #34739 > unrolled thread
| Started by | justaguy <lichunshen84@gmail.com> |
|---|---|
| First post | 2018-02-23 04:00 -0800 |
| Last post | 2018-02-23 05:45 -0800 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.javascript
What's wrong with the following uncheck checkbox code? justaguy <lichunshen84@gmail.com> - 2018-02-23 04:00 -0800
Re: What's wrong with the following uncheck checkbox code? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-02-23 13:22 +0100
Re: What's wrong with the following uncheck checkbox code? justaguy <lichunshen84@gmail.com> - 2018-02-23 05:45 -0800
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2018-02-23 04:00 -0800 |
| Subject | What's wrong with the following uncheck checkbox code? |
| Message-ID | <9df167cc-f509-4d11-bb6b-83b34cd3d3e1@googlegroups.com> |
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>
[toc] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2018-02-23 13:22 +0100 |
| Message-ID | <XnsA89288033BD95eejj99@194.109.6.166> |
| In reply to | #34739 |
justaguy <lichunshen84@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)
[toc] | [prev] | [next] | [standalone]
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2018-02-23 05:45 -0800 |
| Message-ID | <01feb27a-470f-4048-a5e0-ce1e75afe376@googlegroups.com> |
| In reply to | #34739 |
On Friday, February 23, 2018 at 7:01:05 AM UTC-5, justaguy wrote:
> 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>
Yeah, the "this" reference of the element's value was my oversight.
Excellent. Thank you.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web