I’m OK

Getting the values in a list

I'm writing some code to work out if a value is in a list. The list is populated by a REST query to the database.

I'm using a jquery selector on the class of the label within each list item. Then iterating the innerHTML to get the value. The code is:

$('.label').each(function (){
if (this.innerHTML == newCategory) categoryExists = true;
});

I can't help feeling that this is a clumsy way of doing it. Is there a simpler way of obtaining the values in a list?
1 person has
this question
+1
This topic is no longer open for comments or replies.
  • Hello! It's a good solution, especially if it works for you.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly

  • The first thing I tried was mapping the database results returned via the REST query service. I mapped $ to a local storage variable called category1Data. When the page loaded, I looked for the local storage variable and there was nothing there.

    I had expected to see the JSON array of database records but there was nothing at all. The local storage variable hadn't even been created. Can you explain why?

  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly

  • I eventually settled on the jquery code: $("[name='label']").each(function () {...

    A bit of background. There is a Label control inside the List control. The Label is mapped to the text value that is returned by the REST service query. The label has the name 'label' (not very imaginative I know). The HTML generated by Appery for each label is like this:


    <div name="label" id="refCategory1_label_0" dsid="label" data-role="appery_label" class="label refCategory1_label " _idx="_0">Pizzas</div>


    When my button tries to add a new value to the list, I check whether the value is already in the list. I use jquery to iterate over the values like this:

    $("[name='label']").each(function () {
    if ($(this).html() == newCategory) categoryExists = true;
    });
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly

  • Hi Tom,

    Thanks for your question and investigation you've done.

    Besides you should to know about default template list item that's exists on the page.

    To avoid selecting and iterating through it, please add narrow selector:
    jQuery("li:not([_tmpl] [name='label']")


    Regards.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly