简体   繁体   中英

onclick event not fired on clicking

i am facing a weired small issue.. the onclick action of radio button is not executed. i tried with onchange event as well, but of no use..

   <label><input type="radio" name="search_type" id="search_type" value="name" onchange="by_name()">Enter the Name </label> 
  <input name="tag" type="text" id="search" maxlength="30">                       
  <label><input type="radio" name="search_type" id="search_type" value="all" onchange="all()">All</label>

and on clicking All,

  function all()
   {
       window.alert('hi');
    }

can you help me with your suggestions.. (the js file is linked as well)

EDIT: It does work with all Demo

First, you are using two different inputs with the same id. You can't do that.

Second, try seperating your HTML from JS : DEMO

HTML:

<label>
    <input type="radio" name="search_type" id="search_type" value="name">Enter the Name</label>
<input name="tag" type="text" id="search" maxlength="30">
<label>
    <input type="radio" name="search_type" id="search_type2" value="all">All</label>

Javascript:

var input1 = document.getElementById('search_type');
var input2 = document.getElementById('search');
var input3 = document.getElementById('search_type2');

input1.onclick = function(){
    alert('input1');
};
input2.onclick = function(){
    alert('input2');
};
input3.onclick = function(){
    alert('input3');
};

Why seperate them?

It's modern and if you have a lot of HTML it's easier to read your javascript if you have it in a seperate file.

$(document).ready(function() 
        {

            $('body').click(function()
            {
                alert('hi');
            });
         });

here change the "body" tag to the ID of the object you want to have the function happen on. make sure you are using Jquery otherwise this wont work

Rename the function. all has a predefined meaning in many browsers making it (effectively, if not actually) a reserved keyword when used with in an intrinsic event attribute.

Non-working version gives "NS_ERROR_ILLEGAL_VALUE: Illegal value".

Working version has all renamed to notall .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM