Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Help with bookmarklet: Checking a checkbox

goathier

New Coder
Hello all,
First of all, im not a developper.
Thanks for any help you can give me.

I need help with checking a checkbox with a bookmarklet.
I made a javascript bookmarklet script that used to work but since a new system update at job, it doesnt work anymore.
Old: javascript:if (document.getElementById("N3:EamTransactionQty:0").value>"0" &&document.getElementById("N3:EamSubInventory:0").value=="COU"){ document.getElementById("N3:EamLocatorAllowed:0").value="%"; document.getElementById("N3:selected:0").checked = true;}


The script recognize the IF part and it still works.
Setting the % value works too.
The only issue is the checked part which gives me an error:

VM6642:1
Uncaught TypeError: Cannot set properties of null (setting 'checked')
at <anonymous>:1:50


I tried many things with firing a click on the checkbox or many ways to check the checkbox.. nothing seems to make it.

Here is the javascript or html part in the browser:

<td class="x1x x50 xmw" width="26" style="border-top-width: 0px;">
<span onchange="handleTableMultiSelection('N3:selected:0');updateTriStateCheckBoxState('N3');"><input title="Sélectionner" name="N3:selected:0" type="checkbox" value="0"></span></td>
<input title="Sélectionner" name="N3:selected:0" type="checkbox" value="0">


I cannot change anything in the html.
Is there any way I could get this checkbox checked ?
Thanks !
 
Please post JS / CSS/ HTML code in proper code tags.
The error message tells you that the element you are trying to get checked does not exist. In other words, at that moment in time there is no element with id N3:selected:0 in your document. You should inspect your checkbox (right mouse click -> Inspect) to see what its actual id is. Perhaps it was changed as part of this "new system update at job".
 
Thanks for replying.
Sorry for not putting the proper tags !

Yes, I get that it doesnt exists as an ID, but how can I fire the check if the element has no ID ?
I tried:
JavaScript:
javascript:document.getElementsByName("N3:selected:0").checked = true;
Which does not provide an error but still does not check the box..

In the original post, I might not put the codes in proper tags but I got the codes from inspecting.
I hope I put the proper code tags this time but I'm unsure if its HTML or CSS.. Im not a developper..
Here is what I get from inspecting the checkbox.

HTML:
<td class="x1x x50 xmw" width="26" style="border-top-width: 0px;">
<span onchange="handleTableMultiSelection('N3:selected:0');updateTriStateCheckBoxState('N3');">
<input title="Sélectionner" name="N3:selected:0" type="checkbox" value="0">
</span>
</td>




Any ways I could get it checked from a javascript: command in the browser ?
Thanks again !
 
You need to realize that an element can have an id or a name, or both, and that these are different things ! Previously you were using getElementById("N3:selected:0") to find your checkbox, which returned null because the checkbox does not have an id but a name: <input title="Sélectionner" name="N3:selected:0" type="checkbox" value="0">

Now, you have changed to using getElementsByName("N3:selected:0") which is the right thing to do. Almost there ! But when you lookup the docs on that function you see that it returns a NodeList collection, which obviously does not have a checked property. You need to get your element from that list. If you change your JS code to

document.getElementsByName("N3:selected:0")[0].checked = true;

it will work (note the added index [0]). And then you just have to hope there are no multiple elements with this name...
 
Woah it works perfectly !
You saved my jobmates and me a lot of clicks !
Oww I did look at the docs you mentioned. Clearly not understandable to me !
I sure will remember that tho and will surely helps in my future bookmarklets.

Thanks a lot !
 
Woah it works perfectly !
You saved my jobmates and me a lot of clicks !
Oww I did look at the docs you mentioned. Clearly not understandable to me !
I sure will remember that tho and will surely helps in my future bookmarklets.

Thanks a lot !
You're welcome. Note that a list with just one element does not coerce into the element itself. You still need to extract that single element 😁
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom