...

View Full Version : Checkbox disabled between two forms



trunder
11-11-2010, 07:43 PM
Hi!
I have two input fields from who one of them i to upload image and other text field to upload image from url .I want to disable one of this fileds and to stay only one .For js code is helping me one friend but him help was enough and the code have some bugs,like fist fileds as active and them when check one has disable and opposite.I want this nly one field to be disable when the page is load but when checkbox is clicked .Sorry for my english .I hope you understand me.


<script type="text/javascript">
window.onload = function(){
var check = document.getElementById('check1');
var file = document.getElementById('file');
var text = document.getElementById('text');
check . onchange = function(){
if( check.checked ){
text.setAttribute('disabled', 'disabled');
file.removeAttribute('disabled');
}
else{
text.removeAttribute('disabled');
file.setAttribute('disabled', 'disabled');
}

};
};

</script>
<input id="check1" type="checkbox" name="check" /> From link!!!<br/>
Poster:<input id="file" type="file" name="poster" />

Poster from link:<input id="text" type="text" name="postlink" /><br/>

glenngv
11-11-2010, 08:09 PM
window.onload = function(){
var check = document.getElementById('check1');
var file = document.getElementById('file');
var text = document.getElementById('text');
check.onclick = function(){
if( this.checked ){
text.disabled = true;
file.disabled = false;
}
else{
text.disabled = false;
file.disabled = true;
}
};

Shorter version:

window.onload = function(){
document.getElementById('check1').onclick = function(){
document.getElementById('text').disabled = this.checked;
document.getElementById('file').disabled = !this.checked;
}
};

trunder
11-11-2010, 08:21 PM
Yes the same.But this is onclick.I want to be when the page is load and you don't click nothing one of fileds to be disable and then when click checkbox to be active this disable filed and other active to be disable.:thumbsup:

glenngv
11-11-2010, 08:28 PM
That's what my solution does. It only disables/enables the fields when user clicks the checkbox.

trunder
11-11-2010, 08:45 PM
Is there any way to be free click at the beginning.

glenngv
11-11-2010, 09:22 PM
What do you mean by free click? Can you elaborate what you exactly need?

What my code does is it does nothing on the fields on page load. Only when the user checks or unchecks the checkbox, the appropriate fields get enabled or disabled.

trunder
11-11-2010, 10:43 PM
<html>
<head>
<script>
function disableField(myField)
{
myField.disabled = true
return true
}
function enableField(myField)
{
myField.disabled = false
return true
}
function onfocusField(myField)
{
if (myField.disabled)
{
myField.blur()
return false
}
return true;
}
// The above functions are generic, the following function is specific to this page
function toggleFields(myField)
{
if (myField.checked)
{
disableField(document.forms["example1"].text1)
disableField(document.forms["example1"].checkbox1)
disableField(document.forms["example1"].select1)
}
else
{
enableField(document.forms["example1"].text1)
enableField(document.forms["example1"].checkbox1)
enableField(document.forms["example1"].select1)
}
document.forms["example1"].text1
}
</script>
</head>
<body>
<form name="example1">
Disable Fields: <input type="checkbox" name="control1" onclick="toggleFields(this)" checked="checked">
<br>
Text Field: <input type="text" name="text1" onfocus="return onfocusField(this)">
<br>
Check box: <input type="checkbox" name="checkbox1" onfocus="return onfocusField(this)" onclick="return onfocusField(this)">
<br>
Select: <select name="select1" onfocus="return onfocusField(this)">
<option></option>
</select>
</form>
<script>
disableField(document.forms["example1"].text1)
disableField(document.forms["example1"].checkbox1)
disableField(document.forms["example1"].select1)
</script>
</body>
</html>

This is the final code who i need.Run it, then get out last script and see what I mean.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum