using parent and children to manipulate items
Hi,
I'm trying to manipulate items in a table using jQuery, using the parent() and children() methods of the "this" object. I think I'm not understanding how to use the parent and children methods correctly.
What I want the code to do is to show the list and button within the <td></td> when clicked on, and then hide it when the newly shown button appears.
You can try my code using the tryit editor on w3school. just copy and paste.
http://www.w3schools.com/cn/html/tryit.asp?filename=tryhtml_basic
Thanks,
Waspinator
- <html>
- <head>
- <style type="text/css">
- .selected{
- background-color: red;
- }
- td{
- width:150px;
- height:100px;
- }
- </style>
-
- <script src="http://code.jquery.com/jquery-latest.min.js"></script>
- </head>
- <body>
- <table border="1px">
- <tr>
- <td onclick="click(this, 1)">
- <p>
- <select style="display: none" onChange="select(this)" name="1">
- <option value="1">option 1</option>
- <option value="2">option 2</option>
- <option value="3">option 3</option>
- </select>
- <button style="display: none" onclick="cancel(this, 1)" >removeClass</button>
- </p>
- </td>
- <td onclick="click(this, 2)">
- <p>
- <select style="display: none" onChange="select(this)" name="2">
- <option value="1">option 1</option>
- <option value="2">option 2</option>
- <option value="3">option 3</option>
- </select>
- <button style="display: none" onclick="cancel(this, 2)" >removeClass</button>
- </p>
- </td>
- </tr>
- <tr>
- <td onclick="click(this, 3)">
- <p>
- <select style="display: none" onChange="select(this)" name="3">
- <option value="1">option 1</option>
- <option value="2">option 2</option>
- <option value="3">option 3</option>
- </select>
- <button style="display: none" onclick="cancel(this, 3)" >removeClass</button>
- </p>
- </td>
- <td onclick="click(this, 4)">
- <p>
- <select style="display: none" onChange="select(this)" name="4">
- <option value="1">option 1</option>
- <option value="2">option 2</option>
- <option value="3">option 3</option>
- </select>
- <button style="display: none" onclick="cancel(this, 4)" >removeClass</button>
- </p>
- </td>
- </tr>
- </table>
-
-
- <script>
- function click(cell, value)
- {
- jQuery(cell).addClass("selected");
- jQuery(cell.children()).show();
- }
-
- function cancel(cell)
- {
- jQuery(cell.parent()).removeClass("selected");
- jQuery(cell.children()).hide();
- }
-
- function select(selected)
- {
- value = selected.options[selected.selectedIndex].value;
- alert(value);
- }
- </script>
-
- </body>
- </html>