Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6361687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:48:35+00:00 2026-05-24T23:48:35+00:00

I’m trying to create a toggle function in Javascript for an HTML checkbox where

  • 0

I’m trying to create a toggle function in Javascript for an HTML checkbox where if the checkbox is checked/unchecked, the javascript detects that with an if statement and executes an action accordingly (in this case, adding/removing a parameter to url and refreshing the page with a new set of results)

Right now, I’m just trying to test the functionality of the javascript toggle by having alerts pop up when the box is checked/unchecked.

Here’s the javascript:

<script type="text/javascript" language="javascript">
function filterToggle(attribName)
{
  var elementII = document.getElementById(attribName);
  if(elementII.checkedTag == "unchecked"){
     elementII.checkedTag = "checked";
     alert("checked");
  }
  else {
    elementII.checkedTag = "unchecked";
    alert("unchecked");
  }
}
</script>

And here’s where the checkboxes are being created multiple times, inside a php foreach loop:

$BODY .= sprintf("<input type=\"checkbox\" onclick=\"filterToggle('%s')\" name=\"%s\" value=\"%s\" id=\"%s\" checkedTag=\"unchecked\"/> %s ",
                 $attribName, $attribName, $attribClass, $attribName, $attribName, $attribName);

So the idea here is the boxes should load on the page unchecked (which they are), and when I check them, an alert should say “checked”, and when I uncheck, and alert should say “unchecked”. Everything here works perfectly, except the fact that when I check the boxes now, they say “unchecked”, and when I uncheck, they say “checked”. So it seems the javascript is changing the value of the checkedTag attribute in the checkboxes and responding accordingly, but it’s not recognizing the fact that the value of the checkbox is initially set to “unchecked”, so that instead of setting it to unchecked and alerting “unchecked” when first clicked, as it’s currently seemingly doing, it should set it to “checked” and alert “checked”.

Any thoughts here? I can’t figure out why it’s not recognizing that initial value – tried double quotations around the checkedTag value, single quotations, no quotations…same result.

Here’s some example output code in JSFiddle – http://jsfiddle.net/intenex/s45JC/3/, with 3 checkboxes displaying the error described above.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:48:36+00:00Added an answer on May 24, 2026 at 11:48 pm

    Simply don’t add any checked attribute to the elements which should not be checked.

    You are making it much more complicated than it is. Instead of adding a custom attribute, test the checked property of the DOM element:

    Example:

    HTML

    <input type="checkbox" onchange="filterToggle(this)" name="Below $110" value="Price Range" id="Below$110"  />
    

    JavaScript

    function filterToggle(element) {
      if(element.checked){
         alert("checked");
      }
      else {
         alert("unchecked");
      }
    }
    

    DEMO

    Note: Element ids cannot contain spaces.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to loop through a bunch of documents I have to put
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.