?
Solved

Javascript For Loop Question

Posted on 2004-08-24
4
Medium Priority
?
457 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
This video teaches users how to migrate an existing Wordpress website to a new domain.
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…
Suggested Courses

762 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