Solved

Javascript For Loop Question

Posted on 2004-08-24
4
455 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
The viewer will learn how to dynamically set the form action using jQuery.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

689 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