1

I am trying to load my Perl CGI script's content into the HTML page. .load() does not work. Please tell me what's wrong here:

$(document).ready(function() {
  $('input:checkbox').change(function() {
    var nom = $(this).attr("value");
    if ($(this).is(':checked')) {
      alert('okok');
      $('body').load("../cgi-bin/essai.pl");
    } else {
      //$("#" + nom).remove();
    }
  });

I would like:

  • When I check the checkbox (which has as value "toto") load (in Ajax) essai.pl?param=toto
  • When I uncheck this checkbox, remove the content loaded before.

Hope someone has a little time to help me to leave this jQuery nightmare.
Bye.

1 Answer 1

2

You're loading your script's output directly into the body, hence you make all previous body content (including your checkbox(es)) disappear. I assume that is what you experience.

What you should do is have a dedicated block for the script generated content, and use that block as the target of your load function.

HTML:

<body>
<div id="controller">
  <input type="checkbox" name="toto" id="toto"></input> Load / unload content
  </div>
<div id="target">
  Content comes here
  </div>
</body>

JavaScript:

$('input:checkbox').change(function(){
    if ( $(this).is(':checked') ) {
      var target_url = "../cgi-bin/essai.pl?param="+$(this).attr('name');
      $('div#target').load(target_url);
    }
    else {
      $("div#target").html('');
    }
});

I would also refrain from the "../cgi-bin/" type relative path definition and use absolute path instead.

Working example available at http://jsbin.com/izuni4/19

EDIT Seems like I didn't fully grasp what you were trying to achieve. Here is an edited version of the same javascript which appends/removes content blocks triggered by multiple checkboxes. Hope that solves your issue:

http://jsbin.com/izuni4/22/

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

6 Comments

Great, that's what I search for. But I have 4 checkbox, when I check the first checkbox, new div load in #target, but nothing append when I check the second one. Is it because load is not like append ? It can't add more, it erase ?
@eouti: Edited answer to better fit your needs.
jsbin.com/izuni4/22 is exactly what I need !! \n\n I vote for you dude
@András Szepesházi : Are you master in Jquery and JS in general ? Because my script take several seconds to load, I would like to display a loading gif or something like that while it's executing perl code on the server.
@eouti: lol. Me no Jedi. But check this thread for ajax loading indicator solutions: stackoverflow.com/questions/68485/…
|

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.