How can I call a function when page loads with jQuery?

MrObscure

I want to trigger a JavaScript function after the page loads all the elements and I have to echo some HTTP request data into the page somewhere and use JavaScript to get the information from the DOM.

Therefore, I create the function and fire it on page load using jQuery but it didn't call the javascript (I think the DOM hasn't loaded) so how can I fix this?

  <script>
    $( document ).ready(function(){
        myFunction(); // firing the function 
    });
    </script>
    <script>
    function myFunction(){
    var target = document.getElementById("target");
    var formscount = target.textContent(); // the number of items from [#target]
    alert(target);
    if (formscount > 0) {
        active="";
        for (i=0; i < formscount; i++) {
            var str='active'
            if (i > 0){
            str =  ''
            }
            $('#DOM').append($('SOME HTML ELEMENTS');        
            event.preventDefault()
        }
    }
}

</script>

DOM Called (target) for passing the data ($_POST['itemscount']):

<?
$count = $_POST['itemscount'];
?>

<div id="target" style="display:none;">
<?  $output = $_POST['productscount']; // or $Count //
    echo htmlspecialchars($output); 
?>
</div>
Md Shahbaz Ahmad

First, textContent is not a function. So it should be written like this:

var formscount = target.textContent;

Second thing, you are missing the closing bracket ) in line $('#DOM').append($('SOME HTML ELEMENTS');

$('#DOM').append($('SOME HTML ELEMENTS'));  

Third thing, event.preventDefault() will not work, as event is undefined. You may remove it.

and lastly, php tags begins with <?php and not with only <?.

<?php
$count = $_POST['itemscount'];
?>

<div id="target" style="display:none;">
<?php
    $output = $_POST['productscount']; // or $Count //
    echo htmlspecialchars($output); 
?>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I call an API when a website page loads?

How can I call a function when the last image of several images loads?

How can I append dynamic input fields when the page loads?

How can I make this happen when page loads?

How can I activate Reg-Ex when the page loads?

Can I call a Jquery function from an HTML page

How can I call a page when i clicked the button in codeiginiter?

How can show <div> and <a> in ascending order when page loads using jquery?

Run Javascript function when jquery mobile page loads

How can I call click function to another click function in jQuery

Call function when application loads

How can I change colour of text based on its value when a page initially loads?

How can I modify this script so that it works when the page first loads AND on change?

How can I have a Bootstrap button in the disabled state when the page loads?

How can I keep my accordion panels open when the page loads, but maintain its collapsibility afterwards?

run function when page loads

How to execute a function in my view model when page loads

How to run a javascript function when a page dynamically loads content

How can I call a function with a variable and pass it as a parameter? Jquery

How can I call clearInterval outside of a function in jQuery? Outside the setInterval

How can I call function in drop method of jquery?

How can I automatically call a function on load in JQuery?

How i can call function js in enum using javascript/jquery

How can I call the jQuery function: animate from outside the html?

How to hide multiple drop downs when page loads using jQuery?

How to call function when i can't know about the signature?

How can I call a function when the AlertDialog is dismissed?

How can I call a function when the navigation back button is pressed?

How can I call a function when clicked on a button?