Solved

Javascript For Loop Question

Posted on 2004-08-24
4
449 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
  • 2
4 Comments
 
LVL 5

Expert Comment

by:LindzK
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to count occurrences of each item in an array.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now