AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



html-jquery: How to bind onchange and onload event on dropdown together?

I have a select html tag and i want to bind onchange and onload event on same code in jquery. I don´t want to write duplicate code.

html x 24
jquery x 12
Posted On : 2015-12-18 21:47:17.0
profile Rishi Kumar - anyforum.in Rishi Kumar
523188245674
up-rate
4
down-rate

Answers


The best way to fire onload event on dropdown to perform the same task as for onchange event is you can trigger the change function on script loading itself like below:

$(´#dropdown´).change(function(){
//right your code here
}).trigger(´change´);


For example you can refer following POC :

Dropdown.html:
------------------------------------------------

<html>
<head>
<title>Binding onload and onchange event on dropdown-anyforum.in</title>
</head>
<body>
<select id="country" name="country">
<option>India</option>
<option>Nepal</option>
<option>China</option>
<option>Japan</option>
</select>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<script type="text/javascript">
$(´#country´).change(function(){
alert($(this).val());
}).trigger(´change´);
</script>

</body>
</html>


Output:
------------------------------

Posted On : 2015-12-18 22:15:10
Satisfied : 11 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596129554552
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in