How to retrieve fid from API_GetSchema return?

  • 0
  • 1
  • Question
  • Updated 4 years ago
  • Answered

I am using <label> information to match and read field id.

How can I do that?

var instIdx="1.01";

var instStr= $(label).find("label").text();

(instStr.indexOf(instIdx) > 0) {  // this is checking if the field label contents instldx 

trgtFid= $("field").data("id");   // this returns null

console.log(trgtFid); 

}

}

Photo of Kp

Kp

  • 14 Points

Posted 4 years ago

  • 0
  • 1
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,224 Points 20k badge 2x thumb
In reference to this XML fragment in the response to API_GetSchema:


<fields>
  <field id="6" field_type="text" base_type="text">
  <label>Your Label Value</label>
  ...
  </field>
</fields>

This code will return the corresponding id for the field with label "Your Label Value":

var yourIdValue = $("fields label").filter(function() {
  return $(this).text() === "Your Label Value";
}).prev("field").attr("id");
Photo of Kp

Kp

  • 14 Points
That is awesome! If I would like to find label starts with a certain string which past by a variable, where should I put it?

But the code below gives me null.
I am trying to find elements which has a label starting with instIdx.

var trgtFid = $("fields label").filter(function() {
return $(this).text().indexOf(instIdx)>0;
}).prev("field").attr("id");
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,224 Points 20k badge 2x thumb
Debug:

var trgtFid = $("fields label").filter(function(index) {
  console.log(index, $(this).text());
  return $(this).text().indexOf(instIdx);
}).prev("field").attr("id");
Photo of Kp

Kp

  • 14 Points
Got it. I think I did not put all my code here , then I did not work but it works individually great.
since I have a long list of number to go through , I do like this. And it works. Thank you so much for your help.
$.each( records, function(){
var fdlabel=$(this).find("label").text();
 
if (fdlabel.indexOf(instIdx)===0){
  trgtFid = $(this).prev("field").attr("id");
  console.log(trgtFid );
}