Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have something like this

<td class="standaard_klein" align="right">
  <div class="timereg<?php echo $TEL;?>" id="timereg<?php echo $TEL;?>">
    <i contenteditable><?php print(number_format($line2['WERKUREN'], 2, ',','')); ?>
    </i>
  </div>
</td>
<input type="hidden" name="hist_id" id="hist_id<?php echo $TEL;?>" value="<?php echo $line['ID'];?>">
<input type="hidden" name="counter" id="counter" value="<?php echo $TEL;?>">

and this JS function

<script type="text/javascript" language="javascript">
      var counter = document.getElementById('counter').value;
      $('#timereg'+counter).keypress(function(event){
            if(event.keyCode == 13){
            var x = document.getElementById('timereg'+counter).textContent;
            var y = document.getElementById('hist_id'+counter).value;
            $('#timereg'+counter).load('change_timereg.php?newtime=' + x + '&user=<?php echo $_SESSION['loginid'];?>&hist_id='+ y);
            }
       });

</script>

but it get only first "counter" element. How can I modify function to get specific "counter"? $TEL is counter value that I want to get. (usually it's $i but here $TEL were used).

share|improve this question
is the PHP variable $TEL, and all the HTML in your first snippet part of a loop? if so, you have duplicate ID's, which is a no-no – Elias Van Ootegem 37 mins ago
first id should be unique in a document, so don't use id for the counter instead use a class – Arun P Johny 36 mins ago
Yes it's part of the loop...so what should I do? – MrCkobe 19 mins ago

2 Answers

The problems is you are reading the value of counter only once at the page load.
What you need to do is when the enter key is pressed, you need to find out the corresponding counter value.

$('#timereg'+counter).keypress(function(event){
    if(event.keyCode == 13){
        var counter = $(this).closest('td').find(':input[name="counter"]').val();
        var x = document.getElementById('timereg'+counter).textContent;
        var y = document.getElementById('hist_id'+counter).value;
        $('#timereg'+counter).load('change_timereg.php?newtime=' + x + '&user=<?php echo $_SESSION['loginid'];?>&hist_id='+ y);
    }
});
share|improve this answer
tried, not working... – MrCkobe 18 mins ago

Assuming that you write

 <input type="hidden" name="counter" id="counter" value="<?php echo $TEL;?>">

Not only once but inside a loop, you use the same id for many elements, which won't work.

May i suggest a different approach? Give each div related counter and attach a shared class

<div class="timereg<?php echo $TEL;?> shared_class" related-counter="counter<?php echo $tel;?>" id="timereg<?php echo $TEL;?>">
    <i contenteditable><?php print(number_format($line2['WERKUREN'], 2, ',','')); ?>
    </i>
</div>


<input type="hidden" name="counter" id="counter<?php echo $tel;?>" value="<?php echo $TEL;?>">

and then the js:

    $('.shared_class').keypress(function(event){
        if(event.keyCode == 13){
           // ... whatever you want to do
           related_counter = $(this).attr("related-counter");               
           $("#"+related_counter).load('change_timereg.php?newtime=' + x + '&user=<?php echo $_SESSION['loginid'];?>&hist_id='+ y);
        }
    });
share|improve this answer
I like your approach but I can't get it to work...:( – MrCkobe 6 mins ago
Please create a fiddle – Jacob Amerz 3 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.