Post by Joe1234 » Thu Feb 09, 2023 2:54 pm

I have the following code and the select element will not disable. I know the condition is being picked up because it is updating the value.

Code: Select all

       	if (x === "Donate") {
      		document.getElementsByName('item[3]')[0].value="Print a label (FREE).";
       		document.getElementsByName('item[3]').disabled = true;
       		//document.getElementsByName('item[3]').prop( "disabled", true );
       	}
Last edited by Joe1234 on Fri Feb 17, 2023 11:36 am, edited 1 time in total.

v3.0.3.9 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by SohBH » Thu Feb 09, 2023 5:36 pm

Your first code only retrieve first element.

Second code retrieve array of elements with the name attribute value of "item[3]"
Then uses forEach method to iterate over the returned elements, and sets the disabled property to true on each of them, effectively disabling them.

Code: Select all

document.getElementsByName("item[3]").forEach(e => {
      e.disabled = true;
    });

Business Web Development | Content Creation | Analytics and Reporting | SEO


User avatar
Active Member

Posts

Joined
Mon Nov 02, 2020 12:01 am
Location - Malaysia

Post by paulfeakins » Thu Feb 09, 2023 5:50 pm

Joe1234 wrote:
Thu Feb 09, 2023 2:54 pm
I have the following code and the select element will not disable. I know the condition is being picked up because it is updating the value.

Code: Select all

       	if (x === "Donate") {
      		document.getElementsByName('item[3]')[0].value="Print a label (FREE).";
       		document.getElementsByName('item[3]').disabled = true;
       		//document.getElementsByName('item[3]').prop( "disabled", true );
       	}
Are you missing a [0]?

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Guru Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by Joe1234 » Sat Feb 11, 2023 12:38 am

Thanks both. Paul you were right. The crazy thing is I had that there before, but when I did I had "disable" instead of "disabled" and of course that didn't work, and I didn't think to go back and test the [0] again with the "disabled".

v3.0.3.9 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by paulfeakins » Wed Feb 15, 2023 6:59 pm

Joe1234 wrote:
Sat Feb 11, 2023 12:38 am
Thanks both. Paul you were right. The crazy thing is I had that there before, but when I did I had "disable" instead of "disabled" and of course that didn't work, and I didn't think to go back and test the [0] again with the "disabled".
You know this is the sort of thing that ChatGPT could help with, although I gave it a quick go on your post and it didn't spot the [0] directly, but it did come up with a detailed answer:

ChatGPT wrote:The issue with the code could be that you're trying to disable the select element using the disabled property after updating its value. This may not work as expected because the disabled property is a boolean attribute that indicates whether the element is disabled or not, and changing its value directly may not have the desired effect.

Instead, you can try using the setAttribute method to set the disabled attribute to true:

Code: Select all

if (x === "Donate") {
  document.getElementsByName('item[3]')[0].value = "Print a label (FREE).";
  document.getElementsByName('item[3]')[0].setAttribute("disabled", true);
}
Alternatively, you can use the disabled property together with the selectedIndex property to disable the select element and select a default value at the same time:

Code: Select all

if (x === "Donate") {
  var selectElem = document.getElementsByName('item[3]')[0];
  selectElem.selectedIndex = -1; // or any default option index
  selectElem.disabled = true;
}
By setting the selectedIndex property to a default value, you ensure that the select element still displays a value even though it's disabled.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Guru Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by Joe1234 » Fri Feb 17, 2023 11:36 am

eh, unfortunately it requires my real phone number and I never give that out, so I'll never get to experience the myth and legend that is chatgpt.

v3.0.3.9 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by paulfeakins » Fri Feb 17, 2023 8:48 pm

Joe1234 wrote:
Fri Feb 17, 2023 11:36 am
eh, unfortunately it requires my real phone number and I never give that out, so I'll never get to experience the myth and legend that is chatgpt.
Well that's a requirement for pretty much everything these days so good luck on the Internet without giving that out :crazy:

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Guru Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom
Who is online

Users browsing this forum: No registered users and 268 guests