Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set focus? 1

Status
Not open for further replies.

notuserfriendly

Programmer
May 7, 2004
82
0
0
SE
Hi I've got a form that is supposed to show an extra column field if the users makes the a certain choise, this is done by
Code:
if(strcmp($rowP["v$i"],"1") == 0 ){
$on =  "checked ";
}
echo "\t\t<td><input type=\"radio\" name=\"v[$i]\" $on onfocus=\"this.checked;hideReason('v[$i]');return false;\" value=\"on\"></td>\n";


if(strcmp($rowP["v$i"],"-1") == 0 ){
$of = " checked ";
}

echo "\t\t<td><input type=\"radio\" name=\"v[$i]\" $of onfocus=\"this.checked;showReason('v[$i]');return true;\" value=\"off\"></td>\n";

if(strcmp($rowP["v$i"],"0") == 0 ){
$el = " checked ";
}

echo "\t\t<td><input type=\"radio\" name=\"v[$i]\" $el onfocus=\"this.checked;hideReason('v[$i]');return false;\" value=\"else\"></td>\n";

echo "<td id=\"v[$i]\"><INPUT value=\"reason\" name=\"vr[$i]\" size=\"7\"/></td>\n"

This however doesn't go to well with me loading the page.
I use PHP and get the data from an database.
When accessing the page the field
Code:
echo "<td id=\"v[$i]\"><INPUT value=\"reason\" name=\"vr[$i]\" size=\"7\"/></td>\n"
always shows in the beginning. I'm wondering how to be able to set focus to a field when it's checked.

Tried many things but none of them worked, probably because this is kind of new for me.

Code:
function showReason(theTable)
{
     obj = document.getElementsByTagName('td');
      for (i=0; i<obj.length; i++)
     {
          if (obj[i].id == theTable)
          obj[i].style.display = 'block';
     }
}

function hideReason(theTable)
{
     obj = document.getElementsByTagName('td');
      for (i=0; i<obj.length; i++)
     {
          if (obj[i].id == theTable)
          obj[i].style.display = 'none';
     }
}
 
[1] You can use uniform method to pass the name of the radio buttons.
>[tt]hideReason('v[$i]');[/tt]
[tt]hideReason([blue]this.name[/blue]);[/tt]
>[tt]showReason('v[$i]');[/tt]
[tt]showReason([blue]this.name[/blue]);[/tt]
[2] The return true or return false in the onfocus handler is ambiguous in purpose. What is it for? or what effect would it produce? I think you can get rid of that.
[3] The statement again in the onfocus handler:
>[tt]this.checked[/tt]
would return true or false and that's all and is lost to the junk collector. Maybe you mean to set it to checked (or maybe unchecked? in that case it is :
[tt]this.checked=true //or false?[/tt]
[4] The <td> with id contains an input tag _without_ a type. Is that intentional?
 
Thanks for the info!

[1] Thanks for the advice, will make it easier to code :)
[2] Don't know why, removed it :)
[3] set it to true but no change
[4] added type, had missed it, no change

But unfortunately this doesn't solve my problem,
which is to set focus on checkboxes depending on
if they are checked.

/I
 
I would like to be able to do something like this:

Code:
if(strcmp($rowP["v$i"],"0") == 0 ){
[b]$el = "checked onload=\"this.focus()\"";[/b]
}

echo "\t\t<td><input id=\"elv[$i]\" type=\"radio\" name=\"v[$i]\" $el onfocus=\"this.checked=true;hideReason(this.name);\" value=\"else\"></td>\n";
 
The problem is that input element does not support "onload" event. If you want that radio button being focused onload time, you have to write/echo a separate <script> section with window.onload = some anonymous function containing the instruction like document.getElementById(xx).focus() with the xx part determined by the criteria as determined by strcmp()==0.
 
I came up with this and it almost works, there's only one problem, it only gets the element once.
I've put alerts to check it and it only takes the first week. I.e onv43,ofv43 and elv43.

startw=43
endw=52

Code:
for(week=startw; week<endw; week++)
{
if(document.getElementById('onv'+week+']').checked)
{
	document.getElementById('onv'+week+']').focus();
}else if(document.getElementById('ofv'+week+']').checked)
{
	document.getElementById('ofv'+week+']').focus();
}
else if(document.getElementById('elv'+week+']').checked)
{
	document.getElementById('elv['+week+']').focus();
}
}
 
Without looking into deep, unbalanced ']' in the id seems strange. Second, note that focus() is exclusive. One thing focused means every other thing defocused.
 
sorry it was something that happend when trying to
make the code look nicer
Code:
if(document.getElementById('onv['+week+']').checked)
{
	document.getElementById('onv['+week+']').focus();
}elseif(document.getElementById('ofv['+week+']').checked)
{
	document.getElementById('ofv['+week+']').focus();
}
elseif(document.getElementById('elv['+week+']').checked)
{
	document.getElementById('elv['+week+']').focus();
}

ok, so there is no way of selecting many at the same time or after each other?
Because it is possible by the event onfocus. When clicking manually.
 
Meant this actually works, (doesn't check anything though, but just wanted to see if it was possible), and it will be kind of ugly coding every week like this
when I get to next year.

Code:
document.getElementById('onv[45]').focus();
document.getElementById('onv[46]').focus();
document.getElementById('onv[47]').focus();
document.getElementById('onv[48]').focus();
document.getElementById('onv[49]').focus();
document.getElementById('onv[50]').focus();
 
allthough I just noticed that there's something strange in the loop. Since the code after the loop doesn't get executed. Btw it's else if.
sorry for all the typos in the code.
 
Made it, seems as I missed
.checked == true, don't get why it worked once but not in the rest of the loop.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top