glennn.php Posted May 25, 2010 Share Posted May 25, 2010 need a little help implementing a couple of php (or js) variables in this function: function toggle_acuras() { var form = document.getElementById('Acura'); var thanks = document.getElementById('Acura1'); ((form.checked)? Acura1.style.display='block': Acura1.style.display='none'); } where i can use function toggle_cars($variable) // toggle_cars($div_name) ?? // { var form = document.getElementById('$checkbox_name'); var thanks = document.getElementById('$div_name'); ((form.checked) ? $div_name.style.display='block' : $div_name.style.display='none'); } instead of writing 45 js functions... wouldn't this work somehow? the call is made in the checkbox input >> onclick="toggle_acuras();" thanks in advance GN Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 25, 2010 Share Posted May 25, 2010 No, it won't work how you think. You could use PHP to dynamically write the 45 functions, but that is not necessary either. Instead, you should just build a better JS function. I don't see that the var "thanks" is used, but since it is set to an object with a specific ID, I'm assuming you should have used it in the last line instead of specifying the object directly using "Acrura1.style.display". Anyway, this single function should work assuming the forms and divs are all inthe same format where the div ID is the same as the form, except with a "1" appended function toggle_cars(carType) { var form = document.getElementById(carType); var thanks = document.getElementById(carType+'1'); thanks.style.display= (form.checked) ? 'block': 'none'; return; } Then change the function calles to this onclick="toggle_cars('Acura');" Quote Link to comment Share on other sites More sharing options...
glennn.php Posted May 25, 2010 Author Share Posted May 25, 2010 I just found that javascript - i'm a php junkie, not js ) this is where i got "Acura": <input type="checkbox" value="Acura" id="Acura" name="Acura" onclick="toggle_cars('Acura');"> and the div i'm hiding/showing is Acura1, yes. i'll try this and thank you in advance. G Quote Link to comment Share on other sites More sharing options...
glennn.php Posted May 25, 2010 Author Share Posted May 25, 2010 another question related to these divs, Acura1, Audi1, etc... within each div i have a short query calling the models of each of these makes, of course, which means i have ~45 queries in that page, all within hidden divs (initially). i'm certain that those queries are firing anyway, even tho the divs are hidden - is that the case, and is this a bad thing? i notice no slowness, but i surely like to know if what i'm doing is inefficient or not... thanks for you help. GN Quote Link to comment Share on other sites More sharing options...
glennn.php Posted May 25, 2010 Author Share Posted May 25, 2010 this isn't working, but it looks like it should... doesn't carType have to be defined somewhere...? Quote Link to comment Share on other sites More sharing options...
glennn.php Posted May 25, 2010 Author Share Posted May 25, 2010 fixed it. my typo... thanks, mj Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 25, 2010 Share Posted May 25, 2010 another question related to these divs, Acura1, Audi1, etc... within each div i have a short query calling the models of each of these makes, of course, which means i have ~45 queries in that page, all within hidden divs (initially). i'm certain that those queries are firing anyway, even tho the divs are hidden - is that the case, and is this a bad thing? i notice no slowness, but i surely like to know if what i'm doing is inefficient or not... Well, you can either create all the DIVs on page load OR implement AJAX to load only the slected DIV. Each has it's benefits and drawbacks. Loading them all at once, as you are, may take longer for the page to load initially if you have a LOT of data and if you are not loading them efficiently (more on this below). BUt, performance on changing the displayed DIV will typically be faster for the user. As long as you don't have an extraordinary amount of data I would stick to this method. Loading just one DIV and then using AJAX to load new content will make the page load faster, but performance for changing a DIV will degrade since a server request will be needed each time. This is a good method when there is a LOT of data and trying to load it all into the user's browser would not be effective. Having said all that, you should still ensure you are generating the content for all the DIVs efficiently. You state i have ~45 queries in that page You should only be running ONE query to get the data for all the DIVs and then use PHP logic to create the DIVs from the single result set. Running multiple queries like that is horribly inefficinet. If you post the code to get the data and create one of the DIVs, I can provide some sample code. Quote Link to comment Share on other sites More sharing options...
glennn.php Posted May 25, 2010 Author Share Posted May 25, 2010 makes great sense. didn't even think that far ahead. that's what i'll do, thanks much, for all your help. 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.