westman Posted May 20, 2012 Share Posted May 20, 2012 i have this script and its working fully... window.onload = otoload;function otoload() {var oto_update = <?php echo $id ?>;var url = "../oto_work.php";$.post(url,{ oto_update: oto_update } , function(data) {chater = data;if (chater == "") {$("#whatisit").html('<a href="#" title="chat" onclick="return false" onmousedown="javascript:togglechat(\'chat\');"><img src="../image/chat.png" alt="chat" width="44" height="40" /></a>').show();} else { $("#whatisit").html('<a href="#" title="chat" onclick="return false" onmousedown="javascript:togglechat(\'chat\');"><img src="../image/chat2.gif" alt="chat" width="44" height="40" /></a>').show();//$("#mem+chater").class.back3// code needed}});otoload_timer = setTimeout("otoload()",10000);} now i need to add one line of code to finish it. The logic is i have many divs and the id of the divs is mem and a number (mem25 mem268 mem739 mem6 and so on...)the info in the varible "chater" hold numbers separaded by , (25,268,739,6 and so on...)for each number in the varible chater i am trying to make the div class change to back3 any help? Link to comment Share on other sites More sharing options...
Err Posted May 20, 2012 Share Posted May 20, 2012 (edited) If chater is an array you can loop through the numbers and assign back3 every time. var cnt = chater.length;for (var i = 0, i < cnt; i++) { $("#mem"+chater[i]).class.back3;} Edited May 20, 2012 by Err Link to comment Share on other sites More sharing options...
westman Posted May 20, 2012 Author Share Posted May 20, 2012 when i put your code in the place where i wrote// code neededit does not work and i can not see my image Link to comment Share on other sites More sharing options...
dsonesuk Posted May 20, 2012 Share Posted May 20, 2012 var num_list=chater.split(",");for (var i = 0; i < num_list.length; i++) { $("#mem"+num_list[i]).removeClass("whatever_class").addClass("back3");} Link to comment Share on other sites More sharing options...
westman Posted May 20, 2012 Author Share Posted May 20, 2012 with... var num_list=chater.split(",");for (var i = 0; i < num_list.length; i++) { $("#mem"+num_list[i]).removeClass("whatever_class").addClass("back3");} i can see my image but it is not working...i left .removeClass("whatever_class") as it was coz the class will be "back1" or "back2" (set by php diynamicly) Link to comment Share on other sites More sharing options...
westman Posted May 20, 2012 Author Share Posted May 20, 2012 i used...$("#mem"+num_list).removeClass("back2").addClass("back3");and$("#mem"+num_list).removeClass("back1").addClass("back3");and it still did not change Link to comment Share on other sites More sharing options...
dsonesuk Posted May 20, 2012 Share Posted May 20, 2012 Well keep going I'm pretty sure your hit the correct solution pretty soon, it's like we can't do anything this end, as we have no idea what the html code with id, and classes looks like, or any js errors you are getting, so we can't help can we! Link to comment Share on other sites More sharing options...
westman Posted May 20, 2012 Author Share Posted May 20, 2012 am closer....i usedalert(num_list);to see the code on the screen and saw it..in alert(num_list); i saw the number folowed by , (25,)if we can remove the , (25) it would work how do i remove the , ? Link to comment Share on other sites More sharing options...
dsonesuk Posted May 20, 2012 Share Posted May 20, 2012 var num_list=chater.slice(0,-1)num_list=num_list.split(",");for (var i = 0; i < num_list.length; i++) { $("#mem"+num_list[i]).removeClass("whatever_class").addClass("back3");} Link to comment Share on other sites More sharing options...
westman Posted May 20, 2012 Author Share Posted May 20, 2012 i still have the , if the div is called <div id= "mem25">the code at the mo is looking for <div id= "mem25,"> Link to comment Share on other sites More sharing options...
dsonesuk Posted May 20, 2012 Share Posted May 20, 2012 Not its NOT! you made a mistake somewhere, it will never show the comma, the number after the comma will be stored. Link to comment Share on other sites More sharing options...
westman Posted May 20, 2012 Author Share Posted May 20, 2012 omg its working ;)i dont know what happend but thank you so much thank you Link to comment Share on other sites More sharing options...
westman Posted May 20, 2012 Author Share Posted May 20, 2012 my finel edit i now have... window.onload = otoload;function otoload() {var oto_update = <?php echo $trueid ?>;var url = "../livechat/oto_work.php";$.post(url,{ oto_update: oto_update } , function(data) {chater = data;if (chater == "") {$("#whatisit").html('<a href="#" title="T-chat" onclick="return false" onmousedown="javascript:toggletchat(\'tchat\');"><img src="../image/chat.png" alt="T-chat" width="44" height="40" style="border:0;" /></a>').show();//code needed} else { $("#whatisit").html('<a href="#" title="T-chat" onclick="return false" onmousedown="javascript:toggletchat(\'tchat\');"><img src="../image/chat2.gif" alt="T-chat" width="44" height="40" style="border:0;" /></a>').show();var num_list=chater.slice(0,-1)num_list=num_list.split(",");for (var i = 0; i < num_list.length; i++) { $("#mem"+num_list[i]).removeClass("back2").addClass("back3");}}});otoload_timer = setTimeout("otoload()",10000);} and it is working fullyis there a way that i can set all divs to Class back2 without knowing the div id's? Link to comment Share on other sites More sharing options...
dsonesuk Posted May 20, 2012 Share Posted May 20, 2012 var num_list=chater.slice(0,-1)num_list=num_list.split(",");$("div").addClass("back2");for (var i = 0; i < num_list.length; i++) { $("#mem"+num_list).removeClass("back2").addClass("back3");} Link to comment Share on other sites More sharing options...
westman Posted May 20, 2012 Author Share Posted May 20, 2012 but in post 13 where i have...//code neededchater == "" so i do not know the div id's.is it posible to set all div's starting with "mem" to Class back2? Link to comment Share on other sites More sharing options...
dsonesuk Posted May 20, 2012 Share Posted May 20, 2012 $('[id^=mem]').addClass("back2"); Link to comment Share on other sites More sharing options...
westman Posted May 20, 2012 Author Share Posted May 20, 2012 its not working, the div's whitch we do not have the id's for (but starting with mem in the id) are not changeing back to class back2 Link to comment Share on other sites More sharing options...
dsonesuk Posted May 21, 2012 Share Posted May 21, 2012 Yes they do! Link to comment Share on other sites More sharing options...
westman Posted May 21, 2012 Author Share Posted May 21, 2012 got it... $('[id^=mem]').removeClass("back3").addClass("back2"); working well thank you very much for your time again thank you Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now