homepage Welcome to WebmasterWorld Guest from 54.205.160.82
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
textbox enable/disable using javascript
abhishekkaushik




msg:3985094
 10:14 am on Sep 6, 2009 (gmt 0)

Hello there,

I will quickly brief my problem with JavaScript
I am creating few sets of checkbox-texbox pairs upon check status of which the corresponding textbox will be enabled/disabled. I know this can be done by JavaScript but i don't want to create several function for each pairs of checkbox-textbox because i have lots of them...so what i did was something like this
======html=========
<input name="ccontact" type="checkbox" id="ccontact" value="checkbox" onClick="toggleTB(this)"/>
<input name="contact" type="text" id="contact" value="contact" readonly="true" />

Note that name of checkbox is preceding with one 'c' character with name name of textbox

======JavaScript part=======

<script type="text/javascript">
<!--
function toggleTB(what)
{
var theTB = what.name.substr(1); //this removes the preceding 'c'
var td = document.getElementsByName(theTB).name;

if(what.checked)
{

document.td.disabled=0;
}
else{
document.td.disabled=1;
}
}
//-->
</script>

but this is not working pls help!

 

daveVk




msg:3985105
 11:13 am on Sep 6, 2009 (gmt 0)

this
var td = document.getElementsByName(theTB).name;
should be
var td = document.getElementsByName(theTB)[0];
or
var td = document.getElementById(theTB);

abhishekkaushik




msg:3985112
 12:21 pm on Sep 6, 2009 (gmt 0)

Hi Dave,
final code=============
<script type="text/javascript">
<!--
function toggleTB(what)
{
var theTB = what.name.substr(1);
var td = document.getElementsByName(theTB)[0];
if(what.checked)
{
document.install.td.disabled=0;
}
else
{
document.install.td.disabled=1;
}
}
//-->
</script>

should be like above you said but not working, pls let me know statment under if are also correct "install" is the name of the form

daveVk




msg:3985114
 12:34 pm on Sep 6, 2009 (gmt 0)

Try replacing
if(what.checked)
{
document.td.disabled=0;
}
else{
document.td.disabled=1;
}

with

td.disabled = ! what.checked;

note, no prefix (document. OR document.formname ) required on td

abhishekkaushik




msg:3985115
 12:39 pm on Sep 6, 2009 (gmt 0)

thank you very much Dave,
this worked without any problem.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved