Re: Next form element
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: Next form element

From: Thomas 'PointedEars' Lahn <PointedEars@web.de>
Date: Tue Jan 31 2006 - 21:27:58 CET

BootNic wrote:

>> "Evertjan." [...] wrote: [...]
>> Christoph wrote on 30 jan 2006 in comp.lang.javascript:
>>> I know I can do this by setting the form element to 'readonly' but
>>> that's not possible as I'll need to modify that value elsewhere in
>>> my JS.
>>
>> 1 you can write the value of a readonly <input> element with
>> javascript
>>
>> 2 you cannot change the readonlyness of an <input> element with
>> javascript
>
> I thought one could toggle readOnly between true and false.

You can, but as was said correctly, you do not have to in order to change
the value of the respective control.
 
> What would the situation be that this does not work in?

An unsupportive DOM, particulary a DOM that does not implement the
HTMLInputElement interface of the W3C DOM. However, such DOMs are rare
nowadays: this property is supported r/w for those element objects by the
the Gecko DOM, the IE4+ DOM, the Opera 6+ DOM and the KHTML 2.2+ DOM. That
covers practically almost all known HTML user agents, including Mozilla,
Mozilla Firefox, Internet Explorer 4+, Opera 6+, Konqueror 2.2+ and Safari.
It does not cover Netscape 4.x, for example.
 
>> 3 however, you can use "disabled" to your advantage.
>>
>> <input id=x disabled>
>
> <button type="button" onclick="x.readOnly=(x.readOnly)?false:true;
> x.focus(); x.select();">toggle readOnly</button>

0. The button will be useless to users without client-side script support.

1. Referring to an element object by the ID of the element it represents is
   a deprecated and often IE-only practice. If the `button' element is
   descendant of the same `form' element as the `input' element, it is
   better to use this.form.elements['x'] instead.

2. readOnly is a boolean property already.

3. The `button' element is not backwards compatible and it is unnecessary
   here (as the element content is a text node).

  <script type="text/javascript">
    document.write(
        '<input type="button" value="toggle readOnly"'
      + 'onclick="var x = this.form.elements['x'];'
      + ' x.readOnly = !x.readOnly; x.focus(); x.select();">');
  </script>
 
>> <script type="text/javascript">
>> var x = document.getElementById('x')
>> x.value = 'hi world'
>> alert(x.disabled)
>> x.disabled = false
>> </script>
>>
>> Do not mess with focus() to restrain the user!
>

You are not referring to this, so do not quote it.

PointedEars
Received on Tue Feb 7 21:29:14 2006