H. Eberhardt H. Eberhardt - 1 year ago 64
jQuery Question

jQuery HTML only runs when called manually

I have a small problem, that I could not find the solution.

I have the code below and, when my page loads, it doesn't work, but if I open "Console" and type Preenche(); it fills my page.

function Preenche() {


I want to understand What's happening...



I tried to use:


$(document).ready(function () {

$(function () {

in the end and start of document but nothing worked. I'm using ASP.NET MVC5, don't if this matters..

I'm using jQuery: jquery-1.10.2.min.js

Answer Source

It seems your script is declared before the DOM elements with id 'apple' and 'windows' are even loaded. You need to wrap the function either window.onload or $(document).ready()

window.onload = function()  // or $(document).ready(function()
    function Preenche() {


Example : http://jsfiddle.net/xBB5x/12555/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download