how to add an if inside a . append() in Jquery

Asked

Viewed 199 times

0

Good afternoon dear, I have an append that creates a dynamic list, however I need to do an if when it selects an option in specific it activates the if condition that returns a modal, I don’t know jquery, if anyone knows Gradeco a lot, I haven’t found anywhere how to do it.

Follow the append I’m trying to create:

 $("#ulFiltroHomePeriodo").append("<li><a href=\"#\" onclick=\"seHome.atualizaPeriodoLabel('" + o.Value + "', '" + o.Text + "')\" data-value=" + o.Value + ">" + o.Text + "</a></li>")

This is the if I need to do:

 if (o.Value == "porPeriodo") {
                $("#ulFiltroHomePeriodo").on("click", function () {
                    $("#ModalDatasContainer").modal().show();
                });
            }
  • This option is " selected " through a checkbox ?

  • No, it comes from a dynamic dropdown list, that append assembles the list and the.Value are the Enumerators with the options, these options generate dynamic reports, but in this case I need to set an initial date and an end, I created the modal to select the dates, however I don’t know how to make him select only that option inside the if. Currently the modal appear in all options.

1 answer

2

See if that helps you. I created a list of items using the data-value property as well. It checks the value of this prop and displays an Alert if it finds the value:

HTML:

<ul id="#lista"></ul>

Javascript:

$("#lista").append("<li data-value='item1'>Item 1</li>")
$("#lista").append("<li data-value='item2'>Item 2</li>")
$("#lista").append("<li data-value='item3'>Item 3</li>")
$("#lista").append("<li data-value='item4'>Item 4</li>")

$("#lista").find('li').each(function() {
  $(this).on('click', function() {
    var value = $(this).data('value')
    if(value === 'item1') {
      alert('Item 1 encontrado')
    }
  })  
})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.