www.webdeveloper.com
Results 1 to 3 of 3

Thread: recognation of each elements of a list

  1. #1
    Join Date
    Oct 2013
    Posts
    1

    recognation of each elements of a list

    Hi guys, I would like to apply a recognation of each elements of a list without use ID, just using java script with "for". My goal is when I pass the cursor over the elements it shows something, as you're shall imagining it really requires a recognition. No further, my list is:

    HTML Code:
    <ul class="mm">
    					<li class="mm">All Aplicativos</li>
    					<li class="mm" name="asd">Acessorios</li>
    						
    					<li class="mm">Administração</li>
    					<li class="mm">Jogos</li>
    					<li class="mm">Gráficos</li>
    					<li class="mm">Internet</li>
    					<li class="mm">Escritório</li>
    					<li class="mm">Outros</li>
    					<li class="mm">Preferências</li>
    					<li class="mm">Sistema</li>
    				</ul>
    and as javascript I have this:

    Code:
    function subs() {
    	var itens = document.getElementsByClassName("mm");
    	for (var i = 0; i<itens.length; i++) {
    		var def = ["All","Ac","Ad","Ga","Gr","We","Of","Ot","Pr","Sy"];
    		itens[i].onmouseover = function() {
    		switch(itens[i]) {
    			case itens[i] = def[3]:
    			alert ("ok");
    			}
    		};
    	}
    }
    Any help are welcome. Thanks

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,674
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title>123</title>
    <script>
    window.onload=function(){
    var keywords=["All","Ac","Ad","Ga","Gr","We","Of","Ot","Pr","Sy"],
    elems=document.getElementsByClassName("mm");
    for(var i=0;i<elems.length;i++){
    elems[i].onmouseover=function(){
    var txt=this.innerHTML;
    for(var k=0;k<keywords.length;k++){
    var str=eval('/^'+keywords[k]+'/');
    if(str.test(txt)){alert('"'+keywords[k]+'" found!');break;}
    else{continue;}
    }
    }
    }
    }
    </script>
    </head>
    <body>
    <ul class="mm">
    <li class="mm">All Aplicativos</li>
    <li class="mm" name="asd">Acessorios</li>
    <li class="mm">Administração</li>
    <li class="mm">Jogos</li>
    <li class="mm">Gráficos</li>
    <li class="mm">Internet</li>
    <li class="mm">Escritório</li>
    <li class="mm">Outros</li>
    <li class="mm">Preferências</li>
    <li class="mm">Sistema</li>
    </ul>
    </body>
    </html>

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,444
    Nice example 'Padonak'.

    I didn't see much use for the 'alert' display logic except for testing,
    so I modified your code to display in a screen region (optional)

    I also added some logic to show how it could be further modified to create "Yet Another Menu"
    script that bypasses some of the entries.

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title>LI menu</title>
    
    <script type="text/javascript">
    window.onload=function(){
      var keywords = ["NSU Opt","Web","Cod","Ga","Dre","Goo","Of","Ot","NSU","Sy"],
      elems = document.getElementById('divUL').getElementsByClassName("mm");
      for (var i=0; i<elems.length; i++) {
        elems[i].onmouseover = function() {
          var txt = this.innerHTML;
          for (var k=0; k<keywords.length; k++) {
            var str = eval('/^'+keywords[k]+'/');
            if (str.test(txt)) {
    // optional lines for testing
    // var str = keywords[k]+' found!';
    var str = txt + ' found!';
    document.getElementById('debug').innerHTML = str;
              this.style.cursor = 'pointer';
              break;
            } else {
    // optional line with above for testing
    document.getElementById('debug').innerHTML = '';
              continue;
            }
          }
        }
      }
    }
    </script>
    
    <style type="text/css">
     #divUL { width:300px; border:1px solid red; }
     .mm { font-size:2em; list-style-type:none; }
    </style>
    
    </head>
    <body>
    <div id="divUL">
     <ul>
      <li class="mm" onclick="window.location.href='http://optometry.nova.edu'">NSU Optometry</li>
      <li class="mm" onclick="window.location.href='http://www.webdeveloper.com'">Webdeveloper</li>
      <li class="mm" onclick="window.location.href='http://www.codingforums.com'">CodingForums</li>
      <li class="mm">Jogos</li>
      <li class="mm" onclick="window.location.href='http://www.dreamincode.net'">DreamInCode</li>
      <li class="mm" onclick="window.location.href='http://www.google.com'">Google</li>
      <li class="mm">Escritório</li>
      <li class="mm">Outros</li>
      <li class="mm" onclick="window.location.href=http://www.nova.edu">NSU</li>
      <li class="mm">Sistema</li>
     </ul>
    </div>
    
    <p> <div id="debug"></div>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles