Jump to content

Using variable to getElement


phppup
 Share

Recommended Posts

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?

 

 

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

Posted (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 by requinix
two previous posts removed
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 <>

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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)??

Link to comment
Share on other sites

Posted (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 by phppup
Link to comment
Share on other sites

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');
    }
}

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

image.png

image.png

image.png

image.png

:facewall:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.