2

I have this code which can display drop down in div tag of html. In this I have set one function "hello()" to that drop down through jquery but the problem is that its not calling that function.

<body>
<div id="content"></div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">

    $("#content").append('<select id="sel" onchange="fly()"><option value="1">Choose Me</option></select>');
    $("#sel").attr("onchange", "hello()");
    alert($("#sel").attr("onchange"));

    function hello() {
        alert("Hello");
    }

    function fly() {
        alert("Go to fly");
    }
</script>
</body>

7 Answers 7

4

You need this.

$("#sel").change(hello);
Sign up to request clarification or add additional context in comments.

Comments

1
$("#sel").attr("onchange", hello);

EDIT :

In case you missed it in all the near-identical answers, your original problem was that you put your function in quotes "hello()" rather than just using the function name to call it (ie hello)...

Comments

0

You just need to wrap it in a jquery initialization

$(function(){
    $("#content").append('<select id="sel" onchange="fly()"><option value="1">Choose Me</option></select>');
    $("#sel").attr("onchange", "hello()");
    alert($("#sel").attr("onchange"));

    function hello() {
        alert("Hello");
    }

    function fly() {
        alert("Go to fly");
    }
}); 

Comments

0

Use .change():

$("#sel").change(function(){
   hello();
});

Also wrap it in the jquery init function to ensure its ran after DOM has fully loaded:

$(function(){
   $("#content").append('<select id="sel" onchange="fly()"><option value="1">Choose Me</option></select>')
   $("#sel").change(function(){
      hello();
   });
});

1 Comment

This will execute the hello function, passing the return value (undefined) to change().
0

I would suggest that you move:

$("#content").append('<select id="sel" onchange="fly()"><option value="1">Choose Me</option></select>'); 
$("#sel").attr("onchange", "hello()"); 
alert($("#sel").attr("onchange"));

into a document ready function and remove the "s around hello, like so:

$(document).ready(function() {
    $("#content").append('<select id="sel" onchange="fly()"><option value="1">Choose Me</option></select>'); 
    $("#sel").attr("onchange", hello); 
    alert($("#sel").attr("onchange"));
});

This will allow the page to load, and then change the attribute.

Comments

0

You're using jQuery. There's a shortcut for most things. It should be:

var hello = function() {
   alert("Hello");
}
$("#sel").change(hello);

or

$("#sel").change(function() {
 alert("Hello");
});

It should also be in the document ready function. This is so it doesn't try and attach the event before the elements exist.

$(document).ready(function() {
   // code here
});

Comments

0

I can't comment, but as the others said. the problem its that you are using $("#sel").attr("onchange", "hello()"); instead of $("#sel").attr("onchange", hello);

This is because in JavaScript a function is a data type. So you have to work with high order functions

If you want to understand better this I'd suggest to learn about functional programming.

Besides that, you should use jQuery methods to add event listeners. In this case $(.'#sel').change(hello);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.