how to get ID of current record?

I want to save records in a form editable grid automatically when all fields of a record are populated. Clicking on the standard save button fires code like this:

nm_atualiza_line('incluir',1);

To automatically save the record, I can use this code in javascript onchange events. Problem is that I don’t know how to determine the (internal) ID of the current record. I can find this ID when checking the generared code, but I need to determine the ID at runtime.

When focus is on record 1, I want to execute nm_atualiza_line(‘incluir’,1);
When focus is on record 2, I want to execute nm_atualiza_line(‘incluir’,2);
When focus is on record 3, I want to execute nm_atualiza_line(‘incluir’,3);

(mind that when deleting record 2, the 3rd records keeps ID 3)

Any ideas?

Re: how to get ID of current record?

ehmm nice question… i will hope bartho or scott will answer this :slight_smile:

regards
masbro

Re: how to get ID of current record?

Every record should have a hidden ID field, in looking around in firebug on the edited row,

for row 6, I see:


<td id="hidden_field_data_sc_seq6" class="scFormDataOddMult" style="display: none;"> 6 </td>

Hidden field: (row 8 in edit mode)

hidden_field_data_sc_actions8

Has a lot of info as well.

You might have to get the DOM id of the element and grab the value. I am not sure if SC has any functions for this. It may be an internal function.

I would have to play around with this to give you a better answer.

Regards,
Scott.

Re: how to get ID of current record?

This is actually the approach I’m using. I think I’m almost there, but still have one challenge. I narrowed it down to the code below.

Changing the value of the <select> element pops up the name property of the element (“answer2”), like I suspected.
But why does changing the text field return “undefined” instead of “answer1”?

I hope someone with some javascript knowledge can help me out here…


<html>
<header>
 <script type="text/javascript">
 <!--
 function popup() {
  var obj_focus_name = document.activeElement.name;
  alert('focus is on: '+obj_focus_name);
 }
 //-->
 </script>
</header>

<body>
<form action="#">
<input type="text" name="answer1" onchange="popup();">Answer to question 1</input>
<br />
Answer to question 2
<select name="answer2" onchange="popup();">
 <option value="Yes" selected="">Ja </option>
 <option value="No">Nee </option>
 </select>
</form>

</body
</html>


Re: how to get ID of current record?

You should make the following changes:

in your popup function, check to make sure your object is defined, and activeElement is supported by your browser:


if (document.activeElement) {
  var obj_focus_name = document.activeElement.name;
  alert('focus is on: '+obj_focus_name);
}


You should also assign an ‘id’ to all your input(s):

<input id="question1" type="text" name="answer1" onchange="popup();">Answer to question 1</input>

You should also be able to access your object(s) in the popup function using document.getElementById(’’)
I believe your object value is getting lost since the dropdown is the last defined. The DOM cannot separate them as unique objects.

You should be able to setup a trace on your JS and place a breakpoint in your function and review the object settings.

Regards,
Scott.

Re: how to get ID of current record?

Also … you can always test your code at the following:

http://jsfiddle.net/
http://writecodeonline.com/javascript/

Regards,
Scott.