0

I'm new in javascript world and i know how to use if statement. But when i tried to negate the if statement of a radio button if checked, it's not working. This is my code.

<div class="form-group ">
 <label class="form-control-static">Waiting location</label>
 <div class="radio">
  <label><input id="radio_btn" type="radio"> Location 1 </label> 
 </div> 
</div>    

<script>
    if (!(document.getElementById('radio_btn').checked)) {
             alert('This will pop out.');
         }
</script>

i'm getting an error message of "TypeError: document.getElementById(...) is null"

Thanks in advance!

Zenitram.PRO
  • 109
  • 1
  • 9

1 Answers1

1

Your code works, you probably have an error in the html itself, but you know this already from the comments.

I created this fiddle to test it: http://jsfiddle.net/qks8jek1/

<form>
<input type="radio" id="radio_btn" name="sex" value="male">Male</input>
<br/>
<input type="radio" id="radio_btn2" name="sex" value="female" checked>Female</input>
</form>
killexe
  • 357
  • 4
  • 16
  • Hi thanks for replying. on your jsfiddle, the msg pop out even though the id is referring to the radio_btn2. – Zenitram.PRO Jul 27 '15 at 12:29
  • In my fiddle I am checking if the 'radio_btn' is **NOT** checked and in my scenario it isn't. Note: This will only be checked when the page is loaded. I am not sure what you are trying to accomplish. – killexe Jul 27 '15 at 12:33
  • thanks! yes its working on your jsfiddle. my previous statement is incorrect. Maybe i just need to check on my code a lil bit. – Zenitram.PRO Jul 27 '15 at 12:38
  • i tried your code but its not working on my page. i'm still getting TypeError: document.getElementById(...) is null @killex – Zenitram.PRO Jul 27 '15 at 12:45
  • you know, i tried it on an empty php and it worked. I dont really know why its not working on my php. – Zenitram.PRO Jul 27 '15 at 12:49
  • There is a difference between php and javascript. Do you understand the basics of it? HTML is static, so if you want to create something dynamically you use php (serverside). When a user loads a page he just gets the HTML and the javascript code. In your example you are using javascript and not php, so the code is interpreted on the client. Can you explain to me what you want to do exactly ? – killexe Jul 27 '15 at 13:05