PDA

View Full Version : check readonly



kobo1d
07-15-2007, 05:47 PM
Hi !

Is it possible to check if a form input is readonly or not ?
for example i have a form input like this:

<input id="name" type="text" name="name" value="Name" readonly="readonly">

can i check via javascript, onclick, if the input field is readonly or not ?

thx for help

Twey
07-15-2007, 05:50 PM
if(document.getElementById("name").readonly) {
/* . . . */
}

kobo1d
07-15-2007, 06:01 PM
thx for ya fast replie ! works nice...
another small question..

now inside "/* . . . */" .. is it possible to change a value that has been entered in the form input ?
For example if someone puts a name like "John" and now inside the function i want to add a prefix like "guest:" ..

manipulating the value after someone allready clicked the submit button button.. ?

thx again !

DimX
07-15-2007, 06:28 PM
var txtBox = document.getElementById("name");
txtBox.value = "Guest: " + txtBox.value

kobo1d
07-15-2007, 07:41 PM
hey ! thx alot, it works great !!

one more thing...
this line:


if(document.getElementById("name").readonly)

how can i do it the negative way.. i tried


if(!document.getElementById("name").readonly)

but i did not work..


thx again

kobo1d
07-15-2007, 08:29 PM
ok here is more code...

This is the input part inside the form ->

if ($nickname == 'Anonymous') {
$name = "<input id=\"sbCookie\" maxlength=\"22\" class=\"inputWidth\" onFocus=\"clearField(this);\" value=\"Anonymous\" onBlur=\"checkField(this);\" type=\"text\" name=\"".$field."0\">\n\r";
} else {
$name = "<input id=\"sbCookie\" maxlength=\"15\" class=\"inputWidth\" onFocus=\"clearField(this);\" value=\"".$SBTEXT['field_name']."\" onBlur=\"checkField(this);\" type=\"text\" name=\"".$field."0\" readonly=\"readonly\">\n\r";
}

and this is the javascript part which checks the input ->

if(form.elements[val+'0'].readonly) {
var nameBox = form.elements[val+'0'];
} else {
var nameBox = form.elements[val+'0'];
nameBox.value = "Guest: " + nameBox.value
}

the problem is, that i get the "Guest" Prefix every time..
no matter what.. plz help !!

thx

mwinter
07-15-2007, 10:04 PM
if(document.getElementById("name").readonly)


If the control in question is part of a form, don't use the getElementById method. Use the forms and elements collections, instead - there's no need for the control to even have an id attribute except for the purposes of CSS or related label elements.



how can i do it the negative way.. i tried



if(!document.getElementById("name").readonly)


The property name is readOnly; note the capital O, which is very significant.

Hope that helps

kobo1d
07-15-2007, 10:35 PM
The property name is readOnly; note the capital O, which is very significant.

Hope that helps


ineed that helped lol !! that was the problem..
i made the o to an O and everything worked.. amazing :)

thx alot!

Twey
07-15-2007, 10:57 PM
My error, sorry!

vongurdy
01-30-2008, 01:55 AM
Thanks for the menu I really like it, but I am experiencing a little problem.
When I add a rollover image to the page the menu stops respondig. Have you seen this problem before? is there a way around it? Do one script intervines with the other, because once I delete what I have added the menu works fine again. This happens in both browsers FireFox and IE.

also, I tried to comment or remove the remember part of the script so the page wont write a cookie but I get the same results, the menu stops responding. Can you help me Dimx, thanks again.:confused: