Solved

Javascript For Loop Question

Posted on 2004-08-24
4
453 Views
Last Modified: 2006-11-17
I have several input buttons on a web page.  (I'm mimicing a list).

In Javascript:
I would like to be able to traverse every button on the page and if its name has the word sub in it, I don't want to display it.

for each button
   if instr(btn.name, "sub")
      btn.visible = false
   else
      btn.visible = true

Does anyone know how to do something like this in javascript?
0
Comment
Question by:Fraser_Admin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Expert Comment

by:LindzK
ID: 11881167
Here is some javascript, and some example html code to illustrate how to use it

<html>
<head>
  <title></title>
</head>
<script language="javascript">
  var btncnt = 4;
 
  function HideButtons()
  {
    // get an array of all buttons
    var btns = document.getElementsByTagName("input")
      if (btns.length > 0)
      {
        var btn;
        // loop through each button in the array
        for (var cnt = 0; cnt < btns.length; cnt++)
        {
          btn = btns(cnt);
            var btnName = btn.name;
            try
            {
              // if the button name has the word 'sub' in it, then
              // hide the button
              if (btnName.match("sub").length > 0)
              {
                btn.style.visibility = "hidden";
              }
            }
            catch(exception)
            {
            }
        }
      }
  }
</script>
<body onload="HideButtons()">

<input type="button" value="a" name="btn1">
<input type="button" value="b" name="sub2">
<input type="button" value="c" name="btn3">
<input type="button" value="d" name="btn4">


</body>
</html>
0
 
LVL 1

Accepted Solution

by:
mlpk_tyr earned 500 total points
ID: 11881196
hi,
i read your question. the above can be achieved using javascript with <div> tags of html. you can call a method in your window.onLoad which contains all this code

function showbuttons()
{
var allCtrl=document.forms[0].elements
for(i=0;i<=document.forms[0].elements;i++)
{
if (allCtrl[i].name.indexOf('sub')<> -1)  // we get the name of the control and check for the word 'sub' , if not present then -1 is returned
                                                                     // if its there
{

}
}
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 11881275
Hi

the code below loops through each element on the form. If it is either a button or a submit and its name contrains the word "sub" it is hidden otherwise it is displayed


<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<script language="JavaScript">

onload=function() {

  for (var i=0;i<document.forms[0].elements.length;i++) {
   if (((document.forms[0].elements[i].type=="button") || (document.forms[0].elements[i].type=="submit")) && (document.forms[0].elements[i].name.indexOf("sub")!=-1)){
    document.forms[0].elements[i].style.visibility="hidden";
   } else {
     document.forms[0].elements[i].style.visibility="visible";
   }
  }

}
</script>

<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" method="post" action="">

<input type="button" name="submit" value="Submit">
<input type="submit" name="submit2" value="Submit">
</form>
</body>
</html>

Regards

Scott
0
 
LVL 1

Expert Comment

by:mlpk_tyr
ID: 11881310
sorry i lost the control and i continue ,

i read your question. the above can be achieved using javascript with <div> tags of html. or simply like the below.  you can call a method in your window.onLoad which contains all this code

function showbuttons()
{
var allCtrl=document.forms[0].elements
   for(i=0;i<=document.forms[0].elements;i++)
   {
         if (!(allCtrl[i].type='button) &&( allCtrl[i].name.indexOf('sub') > -1)  // we get the name of the control and the control is a button and check for the word 'sub' , if not present then -1 is returned
                                                                     // if its not there
           {
                document.forms[0].elements[allCtrl[i].name].style.visibility='hidden'; // hiding if it's
           }
     }
}


sorry for the inconvinience ,
mlpk_tyr
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Fulfillment API php code sample 1 74
PHP Form Calculate Total Price 10 121
website rewamp 5 50
Downside of adding characters set in ASP pages 6 36
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question