homepage Welcome to WebmasterWorld Guest from 54.161.175.231
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
looping through a comma separated string?
eatspinach




msg:3947592
 11:11 am on Jul 7, 2009 (gmt 0)

hi i have a string stored in a cookie variable that looks like this 825, 123, 654 etc

these numbers are the ids of the buttons already pressed which i want to keep hidden.. i have some javascript to disable one at a time like for example,

$hideMe = 825;

<script type='text/javascript'>
element = document.getElementById("<?php echo $hideMe; ?>");
element.style.display="none";
</script>

but i need this to work when $hideMe is a big long list of numbers seperated by a comma. has anyone any ideas i am hugely confused as usual ha-ha

thanks

 

eatspinach




msg:3947597
 11:25 am on Jul 7, 2009 (gmt 0)

I seemed to have solved my own problem, here is my solution..

<script type='text/javascript'>

var questions="<?php echo $hideMe; ?>";

var arry = questions.split(', ');

for(var i=0; i<arry.length; i++){
element = document.getElementById(arry[i]);
element.style.display="none";
alert(arry[i]);
}

</script>

DrDoc




msg:3947832
 5:23 pm on Jul 7, 2009 (gmt 0)

That's actually better handled by PHP itself.

<style type="text/css">
<?=$hideMe?> { display: none; }
</style>

Now, whether $hideMe is only a single element or contains a comma separated list of elements, they will all be hidden.

eatspinach




msg:3948239
 8:11 am on Jul 8, 2009 (gmt 0)

You are right my way turned out to be a little buggy.. Thanks a million :)

whoisgregg




msg:3948629
 7:39 pm on Jul 8, 2009 (gmt 0)

DrDoc's solution is far more reliable. However, short tags and the shorthand echo (the <?=) are both deprecated and may not be supported in future versions of PHP. Your code will last longer if you go ahead and use the full echo statement:

<?php echo $hideMe; ?>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved