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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

logic seems back to from for string search

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
hi,

I'm trying to lock out specific fields on a form using the following..
Code:
var elem = document.getElementById(formID).elements;
        var excl = 'button,image,label,legend';
        for(var i=0;i< elem.length;i++){ 
            [b]if(excl.search(elem[i].type)){
                elem[i].disabled = true;
            }[/b]
        }

now the above works, but shouldn't the logic be
Code:
if(!excl.search(elem[i].type))

I can't grasp why it works by saying if it finds the element type in the string to disable, when it should be if it doesn't find a match then disable.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Hi

The [tt]search()[/tt] method's parameter should be a [tt]RegExp[/tt].

For your task the [tt]indexOf()[/tt] method sounds more reasonable
:
JavaScript:
[b]if[/b] [teal]([/teal]excl[teal].[/teal][COLOR=darkgoldenrod]indexOf[/color][teal]([/teal]elem[teal][[/teal]i[teal]].[/teal]type[teal])!=-[/teal][purple]1[/purple][teal])[/teal]
  elem[teal][[/teal]i[teal]].[/teal]disabled [teal]=[/teal] [b]true[/b][teal];[/teal]
Anyway, both [tt]search()[/tt] and [tt]indexOf()[/tt] returns the position of the matched/found substring. While the character positions starts at 0 in JavaScript, the value meaning "not found" is -1.

Feherke.
 
OIC - how comes it works then but with back to front logic?

Also I tried your example and it doesn't seem to work?

I also changed it because it should be if it doesn't find a match, so should say
Code:
if (excl.indexOf(elem[i].type)==-1)
but that doesn't work either.

I'm confused!




"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Hi

You want to exclude it those with the enumerated types, right ? So those which are found in the "list". So those for which [tt]indexOf()[/tt] returns other than -1. So my condition is what you need.

But note that not all [tt]form[/tt] elements have [tt]type[/tt] property.

Feherke.
 
I want to disable all form elements except those in the exclude var.

however using indexOf() either diables all of them or none of them whether using == -1 or != -1

So what am I doing wrong?



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
found the answer...
Code:
var elem = document.getElementById(formID).elements;
        var excl = 'button,image,label,legend';
        for(var i=0;i< elem.length;i++){ 
            [b]if(excl.match(elem[i].type) == null){
                elem[i].disabled = true;
            }[/b]
        }

:)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top