2

I have the following Jquery function:

$( document ).ready(function() {
        $(function() {
        $('select[name="CPUmenu"]').change(function(e) {
          let socket = $(this).val();
          $('tbody tr[data-socket]').show();
          if (socket.length) {
            $('tbody tr[data-socket!="' + socket + '"]').hide();
          }
        });
    });
});

Other code:

CPUmenu:

$sql = "SELECT name, socket FROM cpu";
$result = $connection->query($sql);

if ($result->num_rows > 0) {
    echo "<select name='CPUmenu'>";
    echo "<option value=''>CPU</option>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        if ($row["name"] == $namehref) {
        echo "<option value='". $row["socket"] . "' selected>".$row["name"]."</option>";    
        } else {
        echo "<option value='". $row["socket"] . "'>".$row["name"]."</option>";
        }
    }
    echo "</select>";
} else {
    echo "0 results";
}

myTable:

$sql = "SELECT name, price, id, socket, ramslots, maxram, chipset FROM motherboard";
$result = $connection->query($sql);

if ($result->num_rows > 0) {
    echo "<table id='myTable'><thead><tr><th>Motherboard</th><th>Price</th><th>Socket</th><th>Chipset</th><th>Ram Slots</th><th>Max Ram</th></tr></thead>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tbody><tr data-socket='". $row['socket'] . "'><td><a href='https://au.pcpartpicker.com/product/" . $row["id"] . "' target='_blank'>" . $row["name"] . "</a></td><td>" . $row["price"] . "</td><td>" . $row["socket"] . "</td><td>" . $row["chipset"] . "</td><td>" . $row["ramslots"] . "</td><td>" . $row["maxram"] . "</td></tr></tbody>";
    }
    echo "</table>";
} else {
    echo "0 results";
}

I just tried this but no it doesn't work at all

However it only runs whenever i select a new option in "CPUmenu", I would like it to also run once when the page is opened and loaded.

Thanks

Luke Prior
  • 885
  • 2
  • 11
  • 34

3 Answers3

3

You can trigger the change on page load:

$('select[name="CPUmenu"]').tigger('change');

Please Note: $( document ).ready(function() {... and $(function() {... basically the same thing. You can refer document-readyfunction-vs-function for more.

Demo:

$(function() {
   $('select[name="CPUmenu"]').change(function(e) {
      let socket = $(this).val();
      $('tbody tr[data-socket]').show();
      if (socket.length) {
        //$('tbody tr[data-socket!="' + socket + '"]').hide();
      }
      alert('change trigger');
   });
   $('select[name="CPUmenu"]').trigger('change'); // trigger here
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="CPUmenu">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>
Giulio Bambini
  • 4,695
  • 4
  • 21
  • 36
0

I would recommend splitting the function you want to run out, and just triggering it manually. For example:

$( document ).ready(function() {

  // This is the function, ready to be called
  function updateSocket() {
    let socket = $(this).val();
    $('tbody tr[data-socket]').show();
    if (socket.length) {
      $('tbody tr[data-socket!="' + socket + '"]').hide();
    }
  }

  // Save the element for usage later
  let dropdownElement = $('select[name="CPUmenu"]')

  // Set up the event listener for the dropdown
  dropdownElement.change(updateSocket);

  // Also call it manually.
  updateSocket.bind(dropdownElement)();
});

edit: I added bind for this solution. Otherwise, $(this) would have the incorrect context.

Bronzdragon
  • 345
  • 3
  • 13
  • are you sure `$(this)` on a function will catch the right element?? – Mohamed-Yousef Dec 23 '18 at 05:41
  • @Mohamed-Yousef You're right, it wouldn't. Thanks, I've updated the sample. – Bronzdragon Dec 23 '18 at 05:49
  • for your knowledge .. `$(this)` inside the function will refer to `window` or something but not to the element you want .. so to use it a right way .. you need to pass an argument for a function `function updateSocket(Element)` then use `Element` instead of `this` inside the function like `let socket = $(Element).val();` and when you use the function for the element you can then use `updateSocket(this)` .. have a nice day :-) – Mohamed-Yousef Dec 23 '18 at 05:59
  • Sorry.. I tested it and it works without need to add argument(Element).. sorry about that .. Actually I don't know in which case we need to use argument .. anyway keep it in your mind maybe it'll be useful to know one day .. and sorry again :-) – Mohamed-Yousef Dec 23 '18 at 06:10
  • Yup, that's what I added the bind for. It wouldn't have worked the way I wrote it originally. `this` binding can get a little confusing. – Bronzdragon Dec 23 '18 at 06:13
0

firstly, $(document).ready( ... ) and the nested $(function() { ... }) are redundant.

you can move the change function outside the handler and call it immediately:

$(document).ready(function() {
  const menu = $('select[name="CPUmenu"]');
  const menuChange = (function(e) {
    let socket = $(this).val();
    $('tbody tr[data-socket]').show();
    if (socket.length) {
      $(`tbody tr[data-socket!="${socket}"]`).hide();
  }).bind(menu.get());
  menuChange();
  $('select[name="CPUmenu"]').change(menuChange);
});
ic3b3rg
  • 14,629
  • 4
  • 30
  • 53