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 8963147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:17:43+00:00 2026-06-15T16:17:43+00:00

I am working with some JavaScript and Ajax functions. I am going to put

  • 0

I am working with some JavaScript and Ajax functions. I am going to put some code while I explain myself to be more clear.

I have this element: <div id="divTest" onclick="test()">YES</div>

When the user clicks the DIV, the function replaces the "YES" for something like this:

YES <input type="radio" name="testing" value="YES" onclick="test_2(this.value)" checked /> 
NO <input type="radio" name="testing" value="NO" onclick="test_2(this.value)" />

This allows the user to select again the right option. Then I want to replace the radiobuttons with new value, so when the user selects any option, the DIV replaces the "radio" options and displays the value selected (YES OR NO) as it was on the beginning.

At this point everything works perfect.

Here is my problem:

I want the DIV to have the onclick() as the beginning onclick="test()" so I do it from JavaScript giving it the property this way: div.onclick = function() { test();};. The function test() is executed even if the user does not click on the div. It executes both function right away and does not wait until there is a click on it.

Does anyone knows how can I make the function to wait until there is any click on it? Am I giving the onclick property incorrectly?

I hope I made myself clear.

Here are the functions:

function test() {
   var div = document.getElementById("divTest");
   //I DO NOT INCLUDE THE AJAX CODE BUT THE RESPONSE INCLUDE THE RADIO BUTTONS METIONED ABOVE
   div.innerHTML = xml.responseText();
}

function test_2(newValue) {
   div = document.getElementById("newValue");
   div.innerHTML = newValue;
   div.onclick = function() { test(); };
}
  • 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-15T16:17:45+00:00Added an answer on June 15, 2026 at 4:17 pm

    Yes, your problem is with div.onclick = here you are actually assigning a click action to your div! Which will execute when your page loads.

    What you want is to assign an event listener like so.

    div.addEventListener('click' function(){});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whilst working on some generally horrible Javascript code this morning, I came across the
While working with some jQuery ajax, I've run into this problem with Chrome and
All, I'm working with this Ajax tutorial which basically pull some records from a
I´m working on an app using PhoneGap, JQuery I have 2 standard javascript functions.
I'm working on some old AJAX code, written in the dark dark days before
I have been working with AJAX for a while now, but in limited and
When working on some Javascript for a web application, I noticed that I had
I'm working on some ECMAScript/JavaScript for an SVG file and need to get the
I am trying to get some Javascript working in my Rails app. I want
I've been working with some event handling in Javascript and recently decided to migrate

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.