Yuvaraj
Yuvaraj

Reputation: 27

Here i want to load data in ajax hide show div

i want to show or hide data div using ajax

here my code working as a script but i want to load as it as in ajax.

if clicking next button it shows next div.

then you clicking previous button it shows previous div.

my script code as follows:

<script>
$('#next').click(function() {
$('.current').removeClass('current').hide()
    .next().show().addClass('current');
if ($('.current').hasClass('last')) {
    $('#next').attr('disabled', true);
}
$('#prev').attr('disabled', null);
});

$('#prev').click(function() {
$('.current').removeClass('current').hide()
    .prev().show().addClass('current');
if ($('.current').hasClass('first')) {
    $('#prev').attr('disabled', true);
}
$('#next').attr('disabled', null);
});
</script>

my html code as folow:

<h1> QUESTIONS</h1>

<style>
#div{display:none;}
</style>
<button id="prev" disabled="disabled"> PREVIOUS </button>
<button id="next"> NEXT </button>

<form action="<?php echo site_url('pages/answers');?>" method="post">

<div id="main"> 
<?php $i=1; 
$tt=$ques->num_rows();
foreach($ques->result() as $qus)
{   ?>  
<div <?php if($i==1){?>id="div1" class="first current" <?php } else {?> id="div" <?php } if($i==$tt) {?> class="last"<?php }?>>
<input type="hidden" name="q_id[]" value="<?php echo $qus->qst_id; ?>"/> 
  <?php  echo $i.') ' .$qus->qst_questions;?>
   <label>  <input type="radio" name="<?php echo $qus->qst_id;?>" value="A"/><?php echo $qus->qst_option_a; ?></label>
   <label>  <input type="radio" name="<?php echo $qus->qst_id;?>" value="B"/><?php echo $qus->qst_option_b; ?></label>
   <label>  <input type="radio" name="<?php echo $qus->qst_id;?>" value="C"/><?php echo $qus->qst_option_c; ?></label>
   <label>  <input type="radio" name="<?php echo $qus->qst_id;?>" value="D"/><?php echo $qus->qst_option_d; ?></label>

</div>

<?php $i++; }?>
</div>

</form>

here show/hide div using ajax.

how to do that ? please help any one to solve my problem.

here it as an example ['plese see this']

this is loaded in script but i want to load in ajax

Upvotes: 0

Views: 2561

Answers (2)

Tushar
Tushar

Reputation: 87203

Update your Javascript: Update your configurations for AJAX and it should work.

Demo

$('#next').click(function() {
  $('.current').removeClass('current').hide()
    .next().show().addClass('current');
  if ($('.current').hasClass('last')) {
    $('#next').attr('disabled', true);
  }
  $('#prev').attr('disabled', null);
  $.ajax({
    url: "Url",
    success: function(result) {
      $('#main div:visible').html(result);

    },
    error: function(err) {
      console.log($('#main div:visible').attr('id'));
      $('#main div:visible').html('dynamic html content');
    }
  });

});

$('#prev').click(function() {
  $('.current').removeClass('current').hide()
    .prev().show().addClass('current');
  if ($('.current').hasClass('first')) {
    $('#prev').attr('disabled', true);
  }
  $('#next').attr('disabled', null);
  $.ajax({
    url: "Url",
    success: function(result) {
      $('#main div:visible').html(result);
    },
    error: function(err) {
      console.log($('#main div:visible').attr('id'));
      $('#main div:visible').html('dynamic html content');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<button id="prev" disabled="disabled">Prev</button>
<button id="next">Next</button>
<hr />
<div id="main">
  <div id="div1" class="first current">Div 1</div>
  <div id="div2">Div 2</div>
  <div id="div3" class="last">Div 3</div>
</div>

Upvotes: 1

Sreerejith S S
Sreerejith S S

Reputation: 258

$('#next').click(function() {
    $.ajax({url: "Url", 
            success: function(result){
         $('.current').removeClass('current').hide()
                .next().show().addClass('current');
            if ($('.current').hasClass('last')) {
                $('#next').attr('disabled', true);
            }
            $('#prev').attr('disabled', null);
    }});

});

$('#prev').click(function() {
    $.ajax({url: "Url", success: function(result){
 $('.current').removeClass('current').hide()
        .prev().show().addClass('current');
    if ($('.current').hasClass('first')) {
        $('#prev').attr('disabled', true);
    }
    $('#next').attr('disabled', null);
    }});

});

Please write your code on success part and you can return value from server side will get here in 'result' variable You can use 'result' for checking

Upvotes: 0

Related Questions