0

enter image description here

Hello all.

Based on the image above, I have a table that gets its data from a MySql database, I would like to edit a field on that table within the table result. I am learning to use javascript so please be kind. On my table as you can see ID # 16 has an currentbal input field and a image button to click to call my function and pass the value of the ID to it. The ID corresponds to the record ID on my db so I know which record to edit.

Anyway, here is my js function:

function funcEdit(row_id){  
    var currentbal = document.getElementById('currentbal').value;
    alert(currentbal);   
}

It works perfectly for my first row but I am having a problem if I am calling the 2nd row, the currentbal value it still displays is my value on the 1st row. I know I need to change the ID of currentbal ID to make it unique. I was thinking of doing something like this:

<input name="currentbal(add the id here)" type="text" id="currentbal(add the id here)" class="input-mini"  />

to make the currentbal ID unique so my currentbal ID will become currentbal16 or currentbal63 respectively. My problem is how to edit my js function, so that if row 1 is called

var currentbal = document.getElementById('currentbal').value; 

becomes var currentbal = document.getElementById('currentbal16').value;

or var currentbal = document.getElementById('currentbal').value; when row 2 is called. Basically I am having syntax issues I guess. Any guidance would be appreciated.

1 Answer 1

2
var currentbal = document.getElementById('currentbal' + row_id).value;

You simply have to add the numeric id to the end of your 'currentbal' string to make a new string. Then search for that id.

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

1 Comment

Thank you very much. I knew I needed to add the row_id to the ID but my syntax wasn't correct so I can't get it to work =)

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.