Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7731695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:32:50+00:00 2026-06-01T06:32:50+00:00

Two things I’m trying to achieve and need help with 1) Click a link

  • 0

Two things I’m trying to achieve and need help with

1) Click a link and load it in any div on the page, specified within the link itself, which I can do by this:

<div id="box">      
    <a href="test1.php" data-target-div="target1" class="menu-item">Item1</a>
    <a href="test2.php" data-target-div="target2" class="menu-item">Item2</a>     
    <a href="form.html" data-target-div="target1" class="menu-item">Open Form</a>     
</div>
<div id="target1"></div>  
<div id="target2"></div>  

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">


     $(document).ready(function(){

          $('a.menu-item').click(function(e){

            e.preventDefault(); // prevent default click event (equivalent to 'return false;')

            var targetDiv = $(this).data('target-div'); // $(this) is a reference to the menu item we clicked

            var href = $(this).attr('href');                
            console.log(href);  // Write to the console to check we got the href attribute (hit F12 in your browser)

            $("div#"+targetDiv+"").load(href);

            // Note: .load won't work across domains, so both this page and the pages you're loading need to be on the same domain.
            // See this link for a possible work around: 
            // http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin

          });                  

     });

</script>  

2) I want to create a single jQuery/Ajax function that handles all my forms, by picking up the values from the form itself. Something like this:

$(function() {

jQuery(document).ready( function($) {

var $form = $('Figure out which form I mean by finding the ID');

$form.submit( function() {
    $.ajax({
    type: 'POST',
    url: $form.attr( 'action' ), 
    data: $form.serialize(),
    success: function( response ) {
        alert(response);
    }
    });

    return false;
});   
});


});

And finally: Instead of that alert(response); section, display the results in a specified DIV, similar to the Point 1

Still quite new to jQuery so please don’t dumb it down for me 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:32:51+00:00Added an answer on June 1, 2026 at 6:32 am

    For point #2, it’s quite simple, and you’re almost there.

    Try this

    Javascript

    $(document).ready(function(){
        //Target all form tags
        $("form").submit( function() {
            //Get the target div to fill response, assume it's an id
            $div = $("#"+$(this).data("result-div"));
    
            //Do the ajax, and reference `$(this)` for this instance that was submitted
            $.ajax({
                type: $(this).attr('method'),
                url: $(this).attr('action'), 
                data: $(this).serialize(),
                success: function( response ) {
                    $div.html(response);
                }
            });
    
            return false;
        }); 
    });
    

    HTML

    <div id='div_1'></div>
    <div id='div_2'></div>
    <div id='div_3'></div>
    <form method='post' action='url_1.php' data-result-div='div_1'>
        <input type='hidden' name='data_1' value='1' />
        <input type='hidden' name='data_2' value='2' />
        <input type='hidden' name='data_3' value='3' />
        <input type='hidden' name='data_4' value='4' />
        <input type='submit' />
    </form>
    <form method='post' action='url_2.php' data-result-div='div_2'>
        <input type='hidden' name='data_5' value='1' />
        <input type='hidden' name='data_6' value='2' />
        <input type='hidden' name='data_7' value='3' />
        <input type='hidden' name='data_8' value='4' />
        <input type='submit' />
    </form>
    <form method='post' action='url_3.php' data-result-div='div_3'>
        <input type='hidden' name='data_9' value='1' />
        <input type='hidden' name='data_10' value='2' />
        <input type='hidden' name='data_11' value='3' />
        <input type='hidden' name='data_12' value='4' />
        <input type='submit' />
    </form>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page that does two things: When users click on this link:
I'm trying to achieve two things, both of which I fail to get at.
I'm trying to get two things done after a user clicks on a link:
I'm trying to have two things happen when I click on a button in
I am trying to do two things: Append a div to the body Make
I'm trying to do two things -> First I need to read in an
I'm trying to create a page using ASP.NET MVC 2 which shows two things:
I have two things I need this for, these should explain what I mean:
I want to do two things in my browser: Load a text file into
I would like to do two things within my Android app : Check if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.