javascript - If = one class, but has two classes -


i have validation script validates 2 different classes. want add additional class select boxes generate array onmouseover. problem is, select boxes have 2 classes, 1 validation, 1 array, , script doesn't recognize unless has exact name. if using == doesn't work @ all, using = tries validate elements on page, without classes.

i'm wondering how can make efficient without breaking rest of pages using script.

this example of html

<div class="select-styled">     <select id="pm" name="pm" class="selvalidate pmpop" onmouseover="pmpop();">         <option value="foo">foo</option>         <option value="">- remove -</option>     </select> </div> 

here validation script

for (i=0; i<thisform.elements.length; i++) {         if (thisform.elements[i].classname = "selvalidate" || thisform.elements[i].classname == "req" || thisform.elements[i].classname == "valfocus") { //do stuff     } } 

why don't use match()?

if (thisform.elements[i].classname.match('selvalidate')) {     // stuff } 

you can't use assignment operator in if because that...assign. == comparison operator. seem want check if 1 css class set element has many..

so match() work better searches occurrence of string within string, rather comparing 2 string literals see if they're equal.

also, match() can accept regular expressions, can powerful. :)

edit:

per barney's comment, recommend using regular expression (as mentioned earlier) avoid false matches might have similar class name (e.g. selvalidate , selvalidateoff or that).

if (thisform.elements[i].classname.match(/\bselvalidate\b/)) {     // now, stuff } 

both good, really. depends on how narrow want search be.

here nice cheat sheet getting started regular expressions in javascript.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -