Victor Turrisi
Victor Turrisi

Reputation: 721

jQuery to Javascript equivalent

I'm new to JavaScript and also jQuery and have been trying to convert this script:

$(document).ready(function () {
    $('#var').val('value');
    document.forms[0].submit();
});

to use only JavaScript, can anyone explain the equivalent functions in js?

Upvotes: 0

Views: 32

Answers (1)

Tushar
Tushar

Reputation: 87203

Use DOMContentLoaded for equivalent of ready.

The DOMContentLoaded event is fired when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. A very different event - load - should be used only to detect a fully-loaded page. It is an incredibly popular mistake for people to use load where DOMContentLoaded would be much more appropriate, so be cautious.

Use getElementById for id selector.

Returns a reference to the element by its ID.

Javascript Code:

document.addEventListener("DOMContentLoaded", function (event) { // Equivalent of ready
    document.getElementById('var').value = 'value';
    document.forms[0].submit();
});

Upvotes: 1

Related Questions