Fenhopi Posted November 15, 2010 Share Posted November 15, 2010 Hi, I have a loop that I run to retrieve all wall posts. Each post, I want to start with textbox hidden. I have a javascript code for that, but it only does it with the last post in the loop. Is there a good solution for this? Code: while($something = $something){ <table> <tr> <td> echo $wallpost; </td> </tr> <script type="text/javascript"> window.onload = hide_box(); function hide_box(){ document.getElementById('myText').style.visibility='hidden'; } </script> <form name="myForm" id="form1" method="post"> <input type="text" name="comments" id="myText" value="Enter in Text" onclick="this.value='';"/> </form> </table> } Quote Link to comment Share on other sites More sharing options...
btherl Posted November 16, 2010 Share Posted November 16, 2010 I think what you need there is to generate the content of the hide_box() function with the loop. The window.onload and function hide_box() lines will need to be outside the loop. The form also needs to be outside the loop, with only the input elements defined inside the loop. If you're not clear on what I'm going on about, feel free to ask for clarification Quote Link to comment Share on other sites More sharing options...
Fenhopi Posted November 16, 2010 Author Share Posted November 16, 2010 Hello, thanks for replying! Hm, yes, a clarification would be nice. I tried putting the javascript on the outside of the loop, but it still won't load anything else than the last one.. Quote Link to comment Share on other sites More sharing options...
btherl Posted November 16, 2010 Share Posted November 16, 2010 Well there's a number of things that need changing. The basic problem is you have the same code repeated with the same names, and a web browser can't understand that, so it just uses the last one. So you also need to change the names. The structure would be something like this: <form name="myForm" id="form1" method="post"> <?php while ($something = $something) { ?> <input name="myText$something" type="text" id="myText$something"> <?php } ?> </form> <script type="text/javascript"> window.onload = hide_box(); function hide_box() { <?php while ($something = $something) { ?> document.getElementById('myText$something').style.visibility = 'hidden'; <?php } ?> } </script> The important things are: 1. The loops are only on the parts that need repeating. The parts outside the loop must not be repeated. 2. Each input element has a different name, so that each javscript instruction to hide the element can act just on that input element. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.