0

How can I execute this code in HTML on page load?

<script> 
 window.onload = $(function(){
 $("#name1, #name2").val("").attr("disabled",true);
};
</script>

I have tried this code but it does not work.

asked Apr 10, 2013 at 10:49
1
  • 2
    directly write in script. jsfiddle.net/9LXRB no need to write onload function Commented Apr 10, 2013 at 10:53

9 Answers 9

3

Better to use the on() handler:

$(window).on('load', function(){
 $("#name1, #name2").val("").attr("disabled",true);
});

or document.ready() if you aren't waiting for particular elements to load:

$(document).ready(function(){
 $("#name1, #name2").val("").attr("disabled",true);
});
answered Apr 10, 2013 at 10:51
Sign up to request clarification or add additional context in comments.

Comments

2

You just have an extra $( that you don't need, and a missing closing }...

window.onload = function() {
 $("#name1, #name2").val("").attr("disabled",true);
};

Although that gets your code working, you could probably run this when the DOM is ready (which is quicker than waiting for all the images to load)...

$(function() {
 $("#name1, #name2").val("").attr("disabled",true);
});
answered Apr 10, 2013 at 10:51

Comments

1

You can use this when you only want to access your DOM:

$(document).ready(function() { /* code */ });
$(function() { /* code */ }); // shorthand function (is identical)

If you require all other resources (styles, scripts, iframes, images, etc.) to be loaded too (eg. get an image dimensions), you need to use this:

$(window).on('load', function() { /* code */ });
answered Apr 10, 2013 at 10:54

Comments

1

You are mixing up JavaScript's way of doing things with jQuery's way of doing things.

Using windows.onload = ... is how you assign a function to be called after the load event occurs in JavaScript.

Using $(function(){...}) is jQuery syntax for $(document).ready(function(){}) which essentialy is the same thing, jQuery's document ready also triggers after load but unlike unlike windows.onload before images are loaded.

Use one or the other syntax.

Either use JavaScript like this:

window.onload = function(){
 $("#name1, #name2").val("").attr("disabled",true);
}

Or one of jQuery's alternatives:

$(function(){
 $("#name1, #name2").val("").attr("disabled",true);
})
$(document).ready(function(){
 $("#name1, #name2").val("").attr("disabled",true);
})
$(window).ready(function(){
 $("#name1, #name2").val("").attr("disabled",true);
})
answered Apr 10, 2013 at 10:56

Comments

0

You should close your function codeblock like this.

<script> 
 window.onload = $(function(){
 $("#name1, #name2").val("").attr("disabled",true);
 });
</script>
answered Apr 10, 2013 at 10:52

Comments

0

Use This, Remove window.OnLoad

$(function(){
 $("#name1, #name2").val("").attr("disabled",true);
});

SEE DEMO

http://jsfiddle.net/qgPzG/

answered Apr 10, 2013 at 10:54

Comments

0

You could use this syntax:

$(function(){
 $("#name1, #name2").val("").attr("disabled",true);
});
answered Apr 10, 2013 at 10:54

Comments

0

Try this :

window.onload = function () {
// do stuff here
 $("#name1, #name2").val("").attr("disabled",true);
}
answered Apr 10, 2013 at 10:55

Comments

0

You forget to close " }); " :

<script> 
 window.onload = $(function(){
 $("#name1, #name2").val("").attr("disabled",true);
 });
</script>
answered Apr 10, 2013 at 10:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.