0

I have created 5 buttons. I'm loading some pages on button click. I want the first button automatically clicked on page load and load it's corresponding html file and the remaining buttons should load html files only on clicking them. Someone help me!!

This is my jquery:

$('a#Home').click(function() {
  $("#home").load("x.html");
});

$('a#Menu1').click(function() {
  $("#menu1").load("y.html");
});

$('a#Menu2').click(function() {
  $("#menu2").load("z.html");
});

$('a#Menu3').click(function() {
  $("#menu3").load("searcharray.html");
});

$('a#Menu4').click(function() {
  $("#menu4").load("sortarray.html");
});
6
  • $('a#Home').trigger('click') api.jquery.com/trigger however better use $("#home").load("x.html"); Commented Apr 10, 2017 at 7:25
  • 1) Call trigger('click'), or click() on the required element 2) DRY up your code Commented Apr 10, 2017 at 7:26
  • }).click(); can be used to click at page load. Commented Apr 10, 2017 at 7:27
  • Why don't you put the link on the href and trigger the Home button on load? Commented Apr 10, 2017 at 7:28
  • @toni plz test the ans that i given. Commented Apr 10, 2017 at 7:40

5 Answers 5

1

Just test this code. I think this will help you.

$( document ).ready(function() {
    console.log( "ready!" );
    $('#btn1').trigger( "click" );
});
function fun1()
{
	alert('loaded');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<button id="btn1" onclick="fun1()">btn1</button>
<button id="btn2">btn1</button>
<button id="btn3">btn1</button>
<button id="btn4">btn1</button>

Sign up to request clarification or add additional context in comments.

Comments

0

trigger the event yourself:

$('a#Home').click(function() {
  $("#home").load("x.html");
});
$('a#home').trigger('click');

Comments

0

Theoretical answer

You can trigger any event on any element using the .trigger() method. This will require you to specify which event you want to fire. More information.

It is also possible to trigger a click event just by calling .click() after binding the event handling. The documentation shows us that we can use this function for both purposes.

$( document ).ready() is an interaction that can be used to run code once when the document has been loaded. More info on that can be found here.

Examples

Example #1 (using .trigger())

$( document ).ready(function() {
    $('a#Home').trigger('click');
});

Example #2 (using .click())

$( document ).ready(function() {
    $('a#Home').click();
});

Comments

0

Assuming the first button is Home then you want run that on document ready by using $(function()

<script>
    $(function() {
        $('a#Home').click();
    });

    function loadHome() {
        $("#home").load("x.html");
    };
</script>

Then update your link to be like:

<a id="Home" onclick="loadHome()">Click Me</a>

1 Comment

I want the first button automatically clicked on page load......Your code does not do that.
0

You can try this:

 <script>
       function pageLoad()
{
    alert('hello');
}

pageLoad();

 </script>


<button id="btn1" onclick="pageLoad()">btn1</button>

Comments

Your Answer

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.