Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find dynamic elements 1

Status
Not open for further replies.

litton1

Technical User
Apr 21, 2005
584
0
0
GB
Hi all

I have dynamic list as below. How can I find the element when the button is clicked? eg.. When id="But2" is clicked how do I find the word electric in id="pd_2"

Thanks.

Code:
 <div class="row" id="row_2">   
    <div class="column" id="PaymentID_2">
        2
    </div> 
    <div class="column">
        NPower
    </div>    
    <div class="column" id="cat_2">
        Utilities
    </div>
    <div class="column" id="pa_2">
        88.88
    </div>
    <div class="column" id="pd_2">
        Electric
    </div>
    <div class="column">
        <input id="But2" type="button" value="Update" class="setPaymentValues" />
    </div>
     <div class="column">
     to-do
    </div>
    </div>
    <div class="row" id="row_1">   
    <div class="column" id="PaymentID_1">
        1
    </div> 
    <div class="column">
        b &amp; Q
    </div>    
    <div class="column" id="cat_1">
        Tools /equipment
    </div>
    <div class="column" id="pa_1">
        20.00
    </div>
    <div class="column" id="pd_1">
        Some material
    </div>
    <div class="column">
        <input id="But1" type="button" value="Update" class="setPaymentValues" />
    </div>
     <div class="column">
     to-do
    </div>
    </div>

Age is a consequence of experience
 
Replace 'But' with 'pd_' and read innerHTML

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Code:
$('input[type="button"]').on('click', function(e){
 e.preventDefault();
 var targetText = 
 $('#' + $(this).attr('id').replace('But','pd_')).text(); alert(targetText);
});
 
Thanks jpadie. I will try this when I get home. Just about to go to work.

Age is a consequence of experience
 
of course you can also pass a value in the onclick event.

Code:
function myFunc(x){
 var txt = document.getElementById('pd_' + x).innerHTML;
 alert(txt);
}
<input id="But1" type="button" value="Update" class="setPaymentValues" onclick="myFunc(1)"/>

or pass this.id and put a bit more intelligence into the function
Code:
function myFunc(x){
 var txt = document.getElementById(x.replace('But','pd_')).innerHTML;
 alert(txt);
}
<input id="But1" type="button" value="Update" class="setPaymentValues" onclick="myFunc(this.id)"/>

or via event listeners (without jquery)
Code:
function initButtons(){
 var elems = document.querySelectorAll('input[type="button"]');
 for(var i = 0; i<elems.length; i++) elems[i].addEventListener('click', getPDText);
}
function getPDText(e){
 e.preventDefault();
 var txt = document.getElementById(this.id.replace('But','pd_')).innerHTML;
 alert(txt);
}
initButtons(); //put this at the bottom of your file, after the DOM is loaded

using the last variant (or the jquery version slightly altered) exposes a method for you to call that will allow the event listeners to be added at any time. so if you add a line to the form dynamically then you can just call initButtons to apply the event listener to the new elements.

or lastly, if that's too much hassle then you can always apply a single listener to the document node and then apply a filter in the callback.

Code:
document.addEventListener('click', myFunc);

function myFunc(e){
    var that = e.target;
    if(that.nodeType == 1){
        if(that.nodeName == 'INPUT'){
            if(that.getAttribute('type') == 'button'){
                e.preventDefault();
                var elem = document.getElementById(that.id.replace('But','pd_'));
                alert(elem.innerHTML);
                return false;
            }
        }
    }
}
The above shows you the long-hand method of filtering (which will work even for really old browser). a short hand could be this

Code:
document.addEventListener('click', myFunc);
function myFunc(e){
  var elems = Array.prototype.slice.call( document.querySelectorAll('input[type="button"]'),0);
  if(elems.indexOf( e.target ) > -1){
       e.preventDefault();
       var txt = document.getElementById(e.target.id.replace('But','pd_')).innerHTML;
       alert(txt);
       return false;
  }
}
 
That's excellent thanks! Works well!

Age is a consequence of experience
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top