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

  • Home
  • SEARCH
  • 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 8080929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:33:53+00:00 2026-06-05T16:33:53+00:00

I am struggling to get a couple of javascripts scripts to work properly. The

  • 0

I am struggling to get a couple of javascripts scripts to work properly.

The first is a simple validation script on the textbox. It works once but if you hit the submit a second time you can proceed without entering anything. How do I make the script to fire (and work) every time the submit button is clicked?

The second thing I’m trying to do is get a div class to change based on a radio click . The problem is that because all the radio buttons share the same id/name it always changes the first radio button background no matter which radio button was clicked. How do I get it to change the class of the div containing it?

HTML looks like (with the javascript in the head tags as shown…

<head>
<link href="/css/0051.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">

function validateForm() {
        var x=document.forms["form"]["txtbox_1501"].value;
    if (x==null || x=="" || x=="Please type your answer in this box")
    {
        document.getElementById("txtbox_container").id = "txtbox_err";
        document.getElementById("ques_txt").className = "ques_txt_err";
        document.getElementById("ques_inst").className = "ques_inst_err";
        return false;
    }
}

function radioDiv() {
    document.getElementById("radio_label").className = "radio_input_selected";
}

</script>

</head>

<body>


<form id="form" name ="form" action="/php/process.php" onsubmit="return validateForm()"  method="post">

<div id="txtbox_container">
       <div id="ques_container">
          <a id="ques_txt" class="ques_txt">Please tell us your name:</br></a>
          <a id="ques_inst" class="ques_inst">(How you would like to be referred to in this demo)</br></a>
       </div>
       <div id="txtbox_input">
          <input type="text" name="txtbox_1501" value="Please type your answer in this box" onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;" /></br>
       </div>
    </div>

    <div id="radio_container">
       <div id="ques_container">
          <a class="ques_txt">What is your gender?</br></a>
          <a class="ques_inst">(Are you female or male?)</br></a>
       </div>
       <label>
          <div id="radio_label" class="radio_input">
             <input type="radio" name="radio_1503" value="2" onclick="return radioDiv()" />
             <a class="radio_label">Female</a>
          </div>
       </label>
       <label>
          <div id="radio_label" class="radio_input">
             <input type="radio" name="radio_1503" value="1" onclick="return radioDiv()" />
             <a class="radio_label">Male</a>
          </div>
       </label></div> 
</div>      

<div id="button">
   <input type="submit" class="submitbutton" value=""/>

</div>  

    </form>

</body>

Any suggestions would be gratefully received. 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-06-05T16:33:54+00:00Added an answer on June 5, 2026 at 4:33 pm

    How do I make the script to fire (and work) every time the submit
    button is clicked?

    The first time you run this line of code:

    document.getElementById("txtbox_container").id = "txtbox_err";
    

    Is properly executed. However, in any following call, you won’t be able to getElementById passing an id which no longer exists! (you just overwrote that element’s id by another one)

    So, getElementById("txtbox_container") returns null from the 2nd call onwards, and trying to set the id property of null yields this fatal error:

    Uncaught TypeError: Cannot set property ‘id’ of null

    Which breaks your function before returning false which makes the form submit normally.

    You can either just comment that line out, or if you need styling change/add a CSS class instead of changing the id.

    How do I get it to change the class of the div containing it?

    You can’t have 2 elements with same id as it’s invalid markup. Instead, pass the reference to the clicked radio button element (this) to your function and get its parentNode to change the styling as you want:

    //HTML
    onclick="return radioDiv(this)"
    //JS
    function radioDiv(el) {
        el.parentNode.className = "radio_input radio_input_selected";
    }
    

    I’m keeping the original class as well, as you probably want to reset the radio elements in case the user selects another radio:

    function radioDiv(el) {
        var radios = document.getElementsByClassName('radio_input');
        //could use a for loop to reset these, but there are only 2 radios...
        radios[0].className = "radio_input"; radios[1].className = "radio_input";
        el.parentNode.className = "radio_input radio_input_selected";
    }​
    

    Here you go.

    JSFiddle Live Demo

    Tips:

    Always validate your HTML, a sturdy DOM structure is a must for any good JS coding. The W3C validation service may be a little too demanding, but any good markup highlighter program can show you unclosed and invalid syntax tags – even the JSFiddle.net online editor.

    Always check your JS console for errors:
    Firefox: Ctrl+Shift+K (or install Firebug and press F12);
    Chrome: Press F12 and select the Console tab.

    This will display errors which may break your code like this one, so you may post it in your questions if you can’t solve them next time. :)

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

Sidebar

Related Questions

I'm currently struggling to get a WebDesignersWall script to function properly on my website.
Struggling to get any list function to work. I've been fine with _show and
I am struggling to get a subsonic select query to work, I am writing
I'm struggling to get past the first stage of getting Game Center integration to
I'm struggling to get offline files working in Chrome. The first view of the
I am struggling to get my interior background image to display in IE, works
I'm struggling to get my head around when to use a couple of the
I am currently struggling to get the following code to compile. First the header
I have been struggling to get a simple autocomplete working with my Rails app
I have been struggling for a couple of hours on a query, but just

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.