How to select a child in JS and use the value you assign it in a function?

Asked

Viewed 27 times

0

       <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Direct Ship File</a>
            <div class="dropdown-menu" id="dsmenu">
                <a class="dropdown-item" href="#" id="RTB1">RTB1</a>
                <a class="dropdown-item" href="#" id="RTB2">RTB2</a>
            </div>
        </li>
 <ul class="corpoDsfile">
    <br>Provide the orderkey below to retrieve the DirectShip file:
    <br> OrderKey:
    <input type="text" name="orderkey" size="100" id="orderkey" autocomplete="off">
    <input type="submit" onkeyup="upHist()" onkeydown="downHist()" onClick="runOrder()">




$('#dsmenu').children().first().click(function dsFile() {
        $(".corpoDsfile").show();
        $(".corpoReport").hide();
        $(".corpoMonitor").hide();
        console.log("RTB1 foi clicado");
        var appid = "RTB1"
});


$('#dsmenu').children().last().click(function dsFile() {
        $(".corpoDsfile").show();
        $(".corpoReport").hide();
        $(".corpoMonitor").hide();
        console.log("RTB2 foi clicado");
        var appid = "RTB2"

});


    function runOrder(appid) {
        $("#error").hide() // hide the error from any previous execution
        var appid = SE RTB1 FOI CLICADO QUERO QUE O APPID SEJA = RTB1
        var appid = SE RTB2 FOI CLICADO QUERO QUE O APPID SEJA = RTB2
  • Good afternoon Felipe, try to be as clear as possible in what you want and in what you found of problem so that we can understand and help. This also includes formatting the code more readable!

1 answer

0


Well, I didn’t really understand your question, but just set the click of the div, because the click of the child runs first...

$(function() {

  let appid;

  $('#dsmenu').click(function() {
    runOrder(appid);
  });

  $('#dsmenu').children().first().click(function dsFile() {
    $(".corpoDsfile").show();
    $(".corpoReport").hide();
    $(".corpoMonitor").hide();
    console.log("RTB1 foi clicado");
    appid = "RTB1"
  });

  $('#dsmenu').children().last().click(function dsFile() {
    $(".corpoDsfile").show();
    $(".corpoReport").hide();
    $(".corpoMonitor").hide();
    console.log("RTB2 foi clicado");
    appid = "RTB2"
  });

  function runOrder(appid) {
    $("#error").hide() // hide the error from any previous execution
    console.log('runOrder(appid), appid=',appid);
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="nav-item dropdown">
  <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Direct Ship File</a>
  <div class="dropdown-menu" id="dsmenu">
    <a class="dropdown-item" href="#" id="RTB1">RTB1</a>
    <a class="dropdown-item" href="#" id="RTB2">RTB2</a>
  </div>
</li>

Browser other questions tagged

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