phppup Posted June 17, 2021 Share Posted June 17, 2021 My problem amounts to tryong to get element using a variable's value instead of a hardcoded name. This works Quote var chosen = document.getElementById("XYZ4").checked; But this has been problematic Quote //other code that produces result = XYZ4; var dee = result; var chosen = document.getElementById(dee).checked; I've tried an exhaustive array of variations with single quotes, double quotes, and + sign combinations, but seem to be missing the bullseye. How can I insert the variable so that var chosen is viable? Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/ Share on other sites More sharing options...
requinix Posted June 17, 2021 Share Posted June 17, 2021 var dee = result; var chosen = document.getElementById(dee).checked; Assuming "result" is a proper variable, that code is correct. Therefore //other code that produces result = XYZ4; this isn't. What is this code? Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587310 Share on other sites More sharing options...
phppup Posted June 18, 2021 Author Share Posted June 18, 2021 (edited) RE-POSTED for clarity and errors ** Ignore the TWO previous similar postings ** It originates from in the beginning of a FOR loop and I then rename it to use within the same loop. At it's inception Quote for ( yada yada yada.....) { alert (loop_item[x]); //displays XYZ4 var dee = loop_item[x]; alert(dee); //displays XYZ4 And I expected dee to work as described. What exactly is a "proper variable"? What would make it improper? Note: when posting code that contained [ ] and lowercase i the code showed incorrectly and altered text to ITALIC. Mgmt may want to address this. Edited June 18, 2021 by requinix two previous posts removed Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587360 Share on other sites More sharing options...
requinix Posted June 18, 2021 Share Posted June 18, 2021 13 minutes ago, phppup said: It originates from in the beginning of a FOR loop and I then rename it to use within the same loop. At it's inception And I expected dee to work as described. Well, there has to be some code somewhere that is not working as expected, because once that "result" makes it to "dee" and getElementById, it no longer works. Stop posting descriptions of your code and post your actual code. 600+ posts now. You should know this. 13 minutes ago, phppup said: Note: when posting code that contained [ ] and lowercase i the code showed incorrectly and altered text to ITALIC. Mgmt may want to address this. Upon discussion with myself, management has decided to address this by pointing out to the user that they should be using the Code <> button when posting code instead of the Quote " button. Again, 600+ posts. Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587363 Share on other sites More sharing options...
phppup Posted June 18, 2021 Author Share Posted June 18, 2021 Quote var inputs = document.getElementsByTagName('input'); for(var x= 0; x< inputs.length; x++) { if(inputs[x].type == 'checkbox') { alert(inputs[x].id); //Create the variable var dee = inputs[x].id; var chosen = document.getElementById(dee).checked; //Do new stuff FAILS at this point UNLESS variable dee is hardcoded with an actual element ID. [Ironically, there are only 2 checkboxes to be assessed, but I thought that this method of coding would "save time" (rather than hardcoded coding)... LOL Of course, it's always good tho learn (at least that's what THEY tell me.] Quote should be using the Code <> button when posting code instead of the Quote " button I would if I could. But when I access from my very very smart phone, the quote is the only available option that resembles <> Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587366 Share on other sites More sharing options...
Barand Posted June 18, 2021 Share Posted June 18, 2021 Rotate the phone to landscape position. The <> button appears on wider screens. Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587367 Share on other sites More sharing options...
phppup Posted June 18, 2021 Author Share Posted June 18, 2021 @Barand I do. Maybe my screen just isn't big enough, but that's how I get the quote option. Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587369 Share on other sites More sharing options...
Barand Posted June 18, 2021 Share Posted June 18, 2021 You can always add code tags manually Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587370 Share on other sites More sharing options...
phppup Posted June 18, 2021 Author Share Posted June 18, 2021 @Barand didn't know that. I'll try it next time. Easier than twisting the phone sideways too. Overall, code has been less problematic (except for this anomaly). Hope things have been well with you. New "official" holiday today: Juneteenth. No other news to report. Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587371 Share on other sites More sharing options...
requinix Posted June 18, 2021 Share Posted June 18, 2021 2 hours ago, Barand said: Rotate the phone to landscape position. The <> button appears on wider screens. Fixed. y'all are weird for posting code from a phone but whatever Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587373 Share on other sites More sharing options...
Barand Posted June 18, 2021 Share Posted June 18, 2021 It would be better if the toolbar switched to two rows instead of losing half of it. Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587374 Share on other sites More sharing options...
requinix Posted June 18, 2021 Share Posted June 18, 2021 Editor is a bit buggy with multiple toolbar rows, but I think I got it. 1 Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587375 Share on other sites More sharing options...
Barand Posted June 18, 2021 Share Posted June 18, 2021 That's a lot better IMO And the buttons still work Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587376 Share on other sites More sharing options...
phppup Posted June 18, 2021 Author Share Posted June 18, 2021 Ummm, back to my original posted question, maybe, please. PS: toolbar looks awesome. Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587378 Share on other sites More sharing options...
Barand Posted June 18, 2021 Share Posted June 18, 2021 3 hours ago, phppup said: New "official" holiday today: Juneteenth. Isn't that tomorrow on the 19th? Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587379 Share on other sites More sharing options...
phppup Posted June 18, 2021 Author Share Posted June 18, 2021 Signed into law yesterday to commemorate June 19, but recognized on the weekday closest to the 19th when 19 falls on the weekend. (This would be interesting to code. LOL) What kind of "observance" could there be if government workers weren't "entitled" to another day off from work (with pay)?? Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587380 Share on other sites More sharing options...
requinix Posted June 18, 2021 Share Posted June 18, 2021 Back to your original posted question, Still getting the impression that you posted a couple snippets of code. What's the complete source? Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587381 Share on other sites More sharing options...
phppup Posted June 18, 2021 Author Share Posted June 18, 2021 (edited) There's ALWAYS more code. But it's mostly HTML or unrelated. Essentially, I'm running through all the input tags and then separating those that are checkboxes. Then, I'm grabbing the ID of the checkboxes and using them [actually only one for now. ie:XYZ4] to "do stuff" depending on whether or not the box is checked. (I'd tell you what happens further down the line, but I haven't coded that far ahead yet. I was testing the waters as I progressed and stopped when I discovered this odd circumstance. However, my notion is to alter color or display a message if checked.) Again, it works when hardcoded and the number of checkboxes is minimal. But it would be nice to be able to have the code effective with each checkbox being evaluated through the 'for loop' (unless there's a better way to accomplish this). Edited June 18, 2021 by phppup Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587383 Share on other sites More sharing options...
Barand Posted June 19, 2021 Share Posted June 19, 2021 Once you learn the difference between a string value and a variable name, you should crack the problem. 1 hour ago, phppup said: (This would be interesting to code. LOL) echo juneteenth(2021); function juneteenth($yr) { $res = new DateTime($yr.'-06-19'); switch ($res->format('w') ) { case 6: return $res->modify("-1 days")->format('Y-m-d'); case 0: return $res->modify("-2 days")->format('Y-m-d'); default: return $res->format('Y-m-d'); } } Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587384 Share on other sites More sharing options...
phppup Posted June 19, 2021 Author Share Posted June 19, 2021 I just KNEW you were going to do that. LOL. However, I'm not sure it's accurate (as if you need something to keep you busy) because if the 19 is on Sunday, the holiday would be Monday. [minus 2 changed to plus 1 ??] Quote difference between a string value and a variable name Care to enlighten me? Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587386 Share on other sites More sharing options...
Barand Posted June 19, 2021 Share Posted June 19, 2021 On 6/17/2021 at 2:43 PM, phppup said: result = XYZ4; versus result = "XYZ4" Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587389 Share on other sites More sharing options...
phppup Posted June 19, 2021 Author Share Posted June 19, 2021 My very first entry on this topic was Quote I've tried an exhaustive array of variations with single quotes, double quotes, and + sign combinations, but seem to be missing the bullseye. I believe my initial effort was document.getElementById("+XYZ4+").checked; That's what necessitated the post. But I can try it again, I suppose. Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587390 Share on other sites More sharing options...
phppup Posted June 19, 2021 Author Share Posted June 19, 2021 OR perhaps I should take a different approach: I have a checkbox that controls a toggled effect on HTML background. var chosen = document.getElementById("XYZ4").checked; I'm considering adding a second checkbox that would have the same effect. Rather than hardcode both (and future) checkboxes, what would be the best way to retrieve an identifier so that it can be inserted as a variable and used to evaluate a condition? Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587393 Share on other sites More sharing options...
requinix Posted June 19, 2021 Share Posted June 19, 2021 Why is this thread still going? Do you need pictures? 6 hours ago, phppup said: Rather than hardcode both (and future) checkboxes, what would be the best way to retrieve an identifier so that it can be inserted as a variable and used to evaluate a condition? Here's some pictures. Quote Link to comment https://forums.phpfreaks.com/topic/312928-using-variable-to-getelement/#findComment-1587399 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.