25. juli 2008 - 16:43Der er
11 kommentarer og 1 løsning
How to find a element in a form?
Hello!
I got a form with several elements. Some are select-tags and they for eg can be looking like: <select name="telefon[0].telefonTyp" size="1"> <option value="">--</option> <option value="1">1</option> <option value="2">2</option> </select>
Hope you can see that the only thing that differ is the names: telefon[0].telefonTyp telefon[1].telefonTyp
I now need to set focus on one or the other in different situations.
I then need to loop through the list of select elements and find the one to set focus to.
I have tried this to first of all find the elements with names that starts with "telefon":
function f_setFocus() { var bFound = false;
// for each form for (f=0; f < document.forms.length; f++) { // for each element in each form for(i=0; i < document.forms[f].length; i++) { if (document.forms[f][i].type == 'select-one') { //Kolla att det är en telefon alert(document.forms[f][i].name); var selectname = document.forms[f][i].name; if (selectname.startsWith('telefon')) { // set the focus to it document.forms[f][i].focus(); var bFound = true; } }
// if found in this element, stop looking if (bFound == true) { break; } }
// if found in this element, stop looking if (bFound == true) { break; } } }
But I only get the error that the object does not support the attribute or method.
I guess it menas this method call: selectname.startsWith('telefon'))
Do any one know how to solve this, to find out if a .name starts with a certain string?
Or do you guys see any other better solution to the whole problem?
Hm, tricky, the framwork that render the elemnts is Strust, I guess, and hope, I can add "id" to elements through Struts.
Is there en easy way to get hold of an element though a collection like: if( form.elements["telefon[1].telefonTyp"] != null) { form.elements["telefon[1].telefonTyp"].setFocus(); }
Back at work on monday and really would love to know before?
function f_setFocus(str,t){ if(elm=document.getElementsByName(str+"["+t+"]."+str+"Typ")[0]){ /* Handle found element */ elm.focus(); }else{ /* Handle unfound element */ alert("The element wasn't found"); } }
// for each form for (f=0; f < document.forms.length; f++) { // for each element in each form for(i=0; i < document.forms[f].length; i++) { if (document.forms[f][i].type == elementFokusInputTyp) { //Check the name var selectname = document.forms[f][i].name; if (selectname.indexOf(elementFokusElementNamn) == 0) { // set the focus to it document.forms[f][i].focus(); var bFound = true; break; } } }
// if found in this element, stop looking if (bFound == true) { break; } } }
Det ligner under alle omstændigheder en dårlig løsning. Udfra dit spørgsmål at dømme, burde mclemens' forslag (26/07-2008 02:39:31) være præcis, hvad du har brug for ... eller også har jeg stadig ikke forstået spørgsmålet =)
Olebole > Min henvendte sig mere til 25/07-2008 22:03:17 end det oprindelige med check på både start af name og type.
Fredrik > Overvej evt. denne: <script type="text/javascript"> function f_setFocus(elementFokusInputTyp, elementFokusElementNamn) { alert('elementFokusInputTyp: ' + elementFokusInputTyp); alert('elementFokusElementNamn: ' + elementFokusElementNamn);
var elms=document.getElementsByTagName(elementFokusInputTyp);
// for each element for(i=0; i < elms.length; i++) { if (elms[i].getAttribute("name").indexOf(elementFokusElementNamn) == 0) { // set the focus to it elms[i].focus(); break; } } } </script>
... Dog burde indexOf(elementFokusElementNamn) == 0 nok laves om til indexOf(elementFokusElementNamn+"[") == 0 (for at den ikke fanger telefonbog[... ved forespørgsel på "telefon")
function f_setFocus(elementFokusInputTyp, elementFokusElementNamn) { var elmentsOfType = document.getElementsByTagName(elementFokusInputTyp);
// for each elements of type of interest for(i=0; i < elmentsOfType.length; i++) { //find correct element to set focus to if (elmentsOfType[i].getAttribute("name").indexOf(elementFokusElementNamn) == 0) { // set the focus to it elmentsOfType[i].focus(); break; } } }
The method is called when tha page is loaded and there is 2 attributes set in the request:
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.