get the value of any clicked td jquery

后端 未结 2 1699
广开言路
广开言路 2021-01-29 10:46

im traying to get the value of any clicked td and show this in a alert() window with jquery or javascript. I was trayin alote of code around the internet \"googling\" but anyone

相关标签:
2条回答
  • 2021-01-29 11:15

    You need to include jQuery library to start working with it. Then just bind a click event to your td and you should see the alert pop up.

    <head>
       <title></title>
    </head>
    <body>
       <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js" type="text/javascript">
    
    // Your code comes here
    

    Also next time if something does not work, the first thing that you are supposed to so id open the console and check if you see any errors and act upon them.

    • Using console.log instead of alert should be the way to go as alert blocks the UI thread completely.

    $("td").click(function() {
      alert($(this).text());
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <table border="1">
      <thead>
        <tr>
          <td>id</td>
          <td>Nombre</td>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>miguel</td>
        </tr>
      </tbody>
    </table>

    0 讨论(0)
  • 2021-01-29 11:30

    Why not attach the click event directly to the td? You also need to make sure you're including jQuery...

    $( "td" ).click(function() {
        alert($(this).text());
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <body>
        <table border="1">
            <thead>
                <tr>
                    <td>id</td>
                    <td>Nombre</td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>1</td>
                    <td>miguel</td>
                </tr>
            </tbody>
        </table>
    </body>

    0 讨论(0)
提交回复
热议问题