Ajith
Ajith

Reputation: 305

Onchange event not working in select box

I added a dropdownlist using seblod extension in joomla. But the javascript is not working for this.

<html>
    <head>
        <script type="text/javascript">
            alert('onload');
            document.getElementById('countrynames').addEventListener('change',function(){
                alert('Hello');
            });
        </script>
    </head>

    <body>
        <select size="1" class="inputbox select " name="countrynames" id="countrynames">
            <option selected="selected" value="">- Select an option -</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
        </select>
    </body>
</html>

Upvotes: 1

Views: 14195

Answers (3)

Zoltan Toth
Zoltan Toth

Reputation: 47687

Move your script to the bottom of the page - after the DOM element you're binding the listener to.

DEMO

<select size="1" class="inputbox select " name="countrynames" id="countrynames">
<option selected="selected" value="">- Select an option -</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>

JS:

<script type="text/javascript">
    alert('onload');
    document.getElementById('countrynames').addEventListener('change',function(){
         alert('Hello');
    });
</script>

Upvotes: 3

Littm
Littm

Reputation: 4947

You may try to put your code in a function which will be called by the <body> tag's onload method:

Your JS script:

<head>
    <script type="text/javascript">

        function mload() {
            alert('onload');
            document.getElementById('countrynames').addEventListener('change',function()      {alert('Hello');

            });
        }
    </script>
</head>

In your <body> add:

<body onload="javascript: mload()">

Hope this helps. Let me know if this works for you.

Upvotes: 0

SReject
SReject

Reputation: 3936

You script is executing before the select element is created. here's the fix:

<html>
    <head>
        <script type="text/javascript">
            window.onload = function(){
                alert('onload');
                document.getElementById('countrynames').addEventListener('change',function(){
                    alert('Hello');
                });
            }
        </script>
    </head>

    <body>
        <select size="1" class="inputbox select " name="countrynames" id="countrynames">
            <option selected="selected" value="">- Select an option -</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
        </select>
    </body>
</html>

Upvotes: 0

Related Questions