jQuery - Id() and show() does not run correctly

Asked

Viewed 100 times

2

I have items of which in a paragraph is displayed the total of them. When I remove one of these items, I am working with 2 paragraphs with the same code so that I hide one and show the other, so that I can display the correct value. However, when I click on the button that is to do this function, only one of them is changed, while the other continues with the original code.

Paragraph code:

<?php foreach($this->getItems() as $_index=>$_item): ?>
<p class="number" id="numberOriginal"><?php echo $this->__('Item %d of %d', $_index+1, $this->countItems()) ?></p>
<p class="number" id="numberModificado" style="display: none;"><?php echo $this->__('Item %d of %d', $_index+1, $this->countItems()-1) ?></p>
<?php endforeach; ?>

Code "triggers" this function:

$j('input[name=isgift]').click(function(){
  if($j('#isgift1').is(':checked')){
  mudarNumber();
  }
});

Function code to change the <p>:

function mudarNumber(){
  $j('#numberOriginal').hide();
  $j('#numberModificado').show();
}

Example:

Item 1 of that list <p> gets that way:

<p class="number" id="numberOriginal" style="display: none;">Item 1 de 3</p>
<p class="number" id="numberModificado" style="">Item 1 de 2</p>

Item 2 of that list <p> gets that way:

<p class="number" id="numberOriginal">Item 2 de 3</p>
<p class="number" id="numberModificado" style="display: none;">Item 2 de 2</p>

1 answer

2


Try to change the code trigger of the function by:

$j('input[name=isgift]').on('click', function () {
  if ($j('#isgift1').is(':checked')) {
    $j('.numberOriginal').hide();
    $j('.numberModificado').show();
  }
});

And the PHP code, switch to:

<?php foreach($this->getItems() as $_index=>$_item): ?>
  <p class="number numberOriginal"><?php echo $this->__('Item %d of %d', $_index+1, $this->countItems()) ?></p>
  <p class="number numberModificado" style="display: none;"><?php echo $this->__('Item %d of %d', $_index+1, $this->countItems()-1) ?></p>
<?php endforeach; ?>

This way, you can discard this piece of code:

function mudarNumber(){
  $j('#numberOriginal').hide();
  $j('#numberModificado').show();
}
  • It worked, thanks for the help! D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.