I've been stuck for hours on a problem that might be actually simple but I can't manage to find the solution.
To be short, I want to know the index (ie the first / the second / the third etc...) of a checkbox when clicking on it.
Here's the jsfiddle showing what's currently working and what is not. I've tried many things but couldn't find the solution I'm looking for.
https://jsfiddle.net/cpydwqk3/2/ <div>(this example applies only for the class "admin" which applies herself on the checkbox "admin").
function usermodif(identifiant) {
alert($('.admin').index(this)); //return -1
}
$("tr").click(function() {
alert($("tr").index(this)); //work but applies on whole line
});
<table>
<caption>Utilisateurs</caption>
<tr>
<th scope="col">pseudo</th>
<th scope="col">points</th>
<th scope="col">points_session</th>
<th scope="col">admin</th>
<th scope="col">banni</th>
</tr>
<tr>
<th scope="row">firstguy</th>
<td><input type="text" name="points" value="45" id="points"></td>
<td><input type="text" name="points_session" value="6" id="points_session"></td>
<td><input type="checkbox" class="admin" name="admin" onclick="usermodif(this.className)"> </td>
<td><input type="checkbox" class="banni" name="banni"> </td>
</tr>
<tr>
<th scope="row">Nico</th>
<td><input type="text" name="points" value="21" id="points"></td>
<td><input type="text" name="points_session" value="21" id="points_session"></td>
<td><input type="checkbox" class="admin" name="admin" onclick="usermodif(this.className)" checked> </td>
<td><input type="checkbox" class="banni" name="banni"> </td>
</tr>
<tr>
<th scope="row">anonyme</th>
<td><input type="text" name="points" value="0" id="points"></td>
<td><input type="text" name="points_session" value="0" id="points_session"></td>
<td><input type="checkbox" class="admin" name="admin" onclick="usermodif(this.className)"> </td>
<td><input type="checkbox" class="banni" name="banni"> </td>
</tr>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
Thanks in advance guys!
Traverse first tp the closest TR. Than get that closest TR index.
TR has an index amongst TBODY TRs, but not the checkbox.
Also, never use inline JavaScript on* attributes, same as you hopefully don't use inline style attributes. JS and CSS should be in one place only, and that's their respective files or tags.
$(".admin").on("click", function() {
const $par = $(this).closest("tr");
alert($par.index());
});
<table>
<tr>
<th scope="row">firstguy</th>
<td><input type="text" name="points" value="45" id="points"></td>
<td><input type="text" name="points_session" value="6" id="points_session"></td>
<td><input type="checkbox" class="admin" name="admin"> </td>
<td><input type="checkbox" class="banni" name="banni"> </td>
</tr>
<tr>
<th scope="row">firstguy</th>
<td><input type="text" name="points" value="45" id="points"></td>
<td><input type="text" name="points_session" value="6" id="points_session"></td>
<td><input type="checkbox" class="admin" name="admin"> </td>
<td><input type="checkbox" class="banni" name="banni"> </td>
</tr>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
Related
<fieldset style="width: 400px;">
<legend><h2>Form Validation</h2></legend>
<form>
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="ime"></td>
</tr>
<tr>
<td>Username</TD>
<td><input type="text" name="username"></td>
</tr>
</table>
<input type="submit" value="Submit" name="send">
</form>
</fieldset>
I am currently making a form inside of a table... This is kind of new for me, since I don't know how to do it. Above is the code I have tried, and I need something like this.
If someone could get me on the right path, ill be really grateful.
Note: As you can see in the picture above, it needs to have colspan of 3, so they can all have equal width. I made a perfect one with just <form>, but i just found out we have to do it inside of a table...
The easiest way to do it is colgroup - there you can specify, how much of space of the table any column should take.
For your example it would be something like this: (plz note that I didn't feel all of needed rows)
<fieldset style="width: 400px;">
<legend><h2>Form Validation</h2></legend>
<form>
<table>
<colgroup>
<col width="40%"/>
<col width="60%"/>
</colgroup>
<tr>
<td>Name:</td>
<td><input type="text" name="ime"></td>
</tr>
<tr>
<td>Username:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Re-type password:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input type="radio" id="male" name="gender" />
<label for="male">Male</label>
<input type="radio" id="female" name="gender" />
<label for="female">Female</label>
<input type="radio" id="other" name="gender" />
<label for="other">Other</label>
</td>
</tr>
<tr>
<td>Programming skills:</td>
<td>
<input type="checkbox" id="java"/>
<label for="java">Java</label>
<input type="checkbox" id="ruby"/>
<label for="ruby">Ruby</label>
<input type="checkbox" id="net"/>
<label for="net">.Net</label>
</td>
</tr>
</table>
<input type="submit" value="Submit" name="send">
</form>
</fieldset>
I have a table where the user can enter some values and transmit these inputs to my backend via a POST request.
Each row consists of a type, brand and some input fields like buyprice, bottom, top, and stop.
For the purpose that I can connect the input data with the backend data it is important to get the information about the brand which is unique in each row.
Because of that I implemented a hidden input field that the necessary pre-defined value.
But when I check my backend, I only receive the data about buyprice, bottom, top and stop. But nothing about the brand.
Here the relevant passage:
<td th:text="${car.getBrand()}">
<input type="text" name="brand" th:value="${car.getBrand()}" hidden>
</td>
Here the complete table (I use thymleaf as a template-engine)
<div class="container">
<br>
<h4 style="text-align: center">Please specify below</h4>
<br>
<form method="POST">
<table id="buyTable" class="table table-hover">
<thead>
<tr>
<th>Type</th>
<th scope="col">Brand</th>
<th scope="col">Buy-Price</th>
<th scope="col">Bottom</th>
<th scope="col">Top</th>
<th scope="col">Stop</th>
<th>Reduce</th>
<th>Start</th>
</tr>
</thead>
<tbody>
<tr th:each="car : ${cars}">
<td>
<select th:id="${car.getBrand()}" title="selectBuyOrSell" onchange="updateBuyTable(this)">>
<option value="buy">Buy</option>
<option value="sell">Sell</option>
</select>
</td>
<td th:text="${car.getBrand()}">
<input type="hidden" name="brand" th:value="${car.getBrand()}">
</td>
<td><input type="number" step="0.00000001" placeholder="Enter price" name="buyPrice" /></td>
<td><input type="number" step="0.00000001" placeholder="Enter bottom" name="bottom" /></td>
<td><input type="number" step="0.00000001" placeholder="Enter top" name="top" /></td>
<td><input type="number" step="0.00000001" placeholder="Enter stop" name="stop" /></td>
<td>
<label class="custom-control custom-checkbox">
<input id="clickboxReduce" type="checkbox" class="custom-control-input"
onclick="handleClickOnStart(this)" checked>
<span class="custom-control-indicator"></span>
</label>
</td>
<td>
<label class="custom-control custom-checkbox">
<input id="clickboxStart" type="checkbox" class="custom-control-input"
onclick="handleClickOnReduce(this)" checked>
<span class="custom-control-indicator"></span>
</label>
</td>
<!-- <td><input id="clickboxReduce" type="checkbox"></td>
<td><input id="clickboxStart" type="checkbox"></td>-->
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td><a style="float:right">Select all</a></td>
<td style="text-align:center"><input type="checkbox" onclick="checkReduce(this)" checked></td>
<td style="text-align:center"><input type="checkbox" onclick="checkInstant(this)" checked></td>
</tr>
</tbody>
</table>
<br/>
<br/>
<button style="float: right!important;" class="btn btn-outline-primary">Continue</button>
<br/>
<br/>
<br/>
<table id="sellTable" class="table table-hover" hidden="true">
<thead>
<tr>
<th>Type</th>
<th>Brand</th>
<th scope="col">Sell-Price</th>
<th scope="col">Amount</th>
</tr>
</thead>
<tbody>
<tr>
</tr>
</tbody>
</table>
</form>
</div>
So it is actually only about getting the brand. Any idea?
Thank you
I figured out that th:text="${car.getBrand()}" within the td tag was overriding the hidden input-field.
So the solution is an a tag and a hidden input within the td tag.
<td>
<a th:text="${car.getBrand()}"></a>
<input type="hidden" name="brand" th:value="${car.getBrand()}"/>
</td>
I have table and I would like to put my td's under th tags. Here is my HTML code:
<table>
<tr>
<th>Your Email</th>
<td>
<input type="text" name="userEmail" value="" id="userEmail" required="">
</td>
</tr>
<tr>
<th>Job Category</th>
//I need break line here
<td>
<label><input type="radio" name="rd1" value="Programmer" id="rd1">Programmer</label><br>
<label><input type="radio" name="rd2" value="Web Developer" id="rd2">Web Developer</label><br>
</td>
<tr>
</table>
My output gives me text in <th> tags and then text in <td> tags next to it. I would like to have td content below th. I tried to use <br> tags but looks like that does not work outside of td tags. If anyone know the best and way to fix this please let me know.
You are creating a table and nothing can be put outside the table cells. If you want to have the forms element under the headers, just create one row with headers and one row with forms elements:
<table>
<tr>
<th>Your Email</th>
<th>Job Category</th>
</tr>
<tr>
<td>
<input type="text" name="userEmail" value="" id="userEmail" required="">
</td>
<td>
<label><input type="radio" name="rd1" value="Programmer" id="rd1">Programmer</label><br>
<label><input type="radio" name="rd2" value="Web Developer" id="rd2">Web Developer</label><br>
</td>
<tr>
</table>
Put your table header in its own row:
<table>
<tr>
<th scope="row">Your Email</th>
<td>
<input type="text" name="userEmail" value="" id="userEmail" required="">
</td>
</tr>
<tr>
<th colspan="2">Job Category</th>
</tr>
<tr>
//I need break line here
<td colspan="2">
<label><input type="radio" name="rd1" value="Programmer" id="rd1">Programmer</label><br>
<label><input type="radio" name="rd2" value="Web Developer" id="rd2">Web Developer</label><br>
</td>
</tr>
</table>
Place them in different rows using
<table>
<tr>
<th scope="row">Your Email</th>
<td>
<input type="text" name="userEmail" value="" id="userEmail" required="">
</td>
</tr>
<tr>
<th>Job Category</th>
</tr>//End row
//I need break line here
<tr> //start new row
<td>
<label><input type="radio" name="rd1" value="Programmer" id="rd1">Programmer</label><br>
<label><input type="radio" name="rd2" value="Web Developer" id="rd2">Web Developer</label><br>
</td>
<tr>
</table>
This seems (and should be) simple, but I have no idea why the values for my radio buttons appear empty
<!DOCTYPE html>
<html>
<body>
<form method="post" action="action_page.php">
<table>
<tr>
<th>Intermediate</th><th>Advanced</th><th>No selection</th>
</tr>
<tr>
<td colspan="3">
<label for="td1">1. Bash</label>
</td>
</tr>
<tr>
<td>
<input type="radio" name="td1" id="td1" value="BashInter"/>
</td>
<td><input type="radio" name="td1" value="BashAdv"/>
</td>
<td><input type="radio" name="td1" value="" /></td>
</tr><tr>
<td colspan="3">
<label for="td1">2. C</label>
</td>
</tr>
<tr>
<td>
<input type="radio" name="td2" id="td2" value="CInter"/>
</td>
<td><input type="radio" name="td2" value="CAdv"/>
</td>
<td><input type="radio" name="td2" value="" /></td>
</tr><tr>
<td colspan="3">
<label for="td1">3. C++</label>
</td>
</tr>
<tr>
<td>
<input type="radio" name="td3" id="td3" value="C++Inter"/>
</td>
<td><input type="radio" name="td3" value="C++Adv"/>
</td>
<td><input type="radio" name="td3" value="" /></td>
</tr></table>
<input type="submit" value="Submit">
</form>
<p>If you click "Submit", the form-data will be sent to a page called "action_page.php".</p>
</body>
</html>
If run on W3Schools' (ugh) server, it correctly outputs the input.
e.g.
td1=BashInter&td2=CAdv&td3=
However, my server, for print_r($_POST); returns
[td1] => [td2] => [td3] =>
regardless of what is selected.
Looking at the HTTP headers confirms that nothing is being sent.
All other aspects of the form correctly send their values.
I have tried a number of variants in relation to the values, but nothing has altered the fact that no data actually appears to be sent by the radio buttons.
You didn't specified that your form must be send as a POST one, so used method is defaulted to GET.
Try with
<form action="action_page.php" method="POST">
Last radio button of every set has been kept empty and has black value.
Which means <input type="radio" name="td3" value="" /> needs to be replaced with <input type="radio" name="td3" value="Some value 3" />
So in case you are selecting last radio button from every set then obviously it wont return anything.
Apart from this there is not other problem as long as your HTML code is concerned. If you still face any problem please upload your PHP code as well.
I also suggest to give some some meaningful label to each radio.
Here is the code you may try
<!DOCTYPE html>
<html>
<body>
<form method="POST" action="action_page.php">
<table>
<tr>
<th>Intermediate</th><th>Advanced</th><th>No selection</th>
</tr>
<tr>
<td colspan="3">
<label for="td1">1. Bash</label>
</td>
</tr>
<tr>
<td>
<input type="radio" name="td1" id="td1" value="BashInter"/> BashInter
</td>
<td><input type="radio" name="td1" value="BashAdv"/> BashAdv
</td>
<td><input type="radio" name="td1" value="Some value 1" /> Some value 1</td>
</tr><tr>
<td colspan="3">
<label for="td1">2. C</label>
</td>
</tr>
<tr>
<td>
<input type="radio" name="td2" id="td2" value="CInter"/> CInter
</td>
<td><input type="radio" name="td2" value="CAdv"/> CAdv
</td>
<td><input type="radio" name="td2" value="Some value 2" /> Some value 2</td>
</tr><tr>
<td colspan="3">
<label for="td1">3. C++</label>
</td>
</tr>
<tr>
<td>
<input type="radio" name="td3" id="td3" value="C++Inter"/> C++Inter
</td>
<td><input type="radio" name="td3" value="C++Adv"/> C++Adv
</td>
<td><input type="radio" name="td3" value="Some value 3" /> Some value 3</td>
</tr></table>
<input type="submit" value="Submit">
</form>
<p>If you click "Submit", the form-data will be sent to a page called "action_page.php".</p>
</body>
</html>
if you try my code below you can see that the background color is light blue and it occupies the whole page of a site, I want to resize it how can you do that? I mean that the color will fit to the same size where the fill up boxes are,
do i need to add this code: width="60%"
can you please tell me where can I add this? Thanks
<html>
<div style="color:red">[+validationmessage+]</div>
<p style="color:#4C4C4C;font-weight:bold">« You can subscribe here: »</p>
<p>[+MailChimp.message+]</p>
<div style="background-color:#CCDFED">
<form method="post" action="[~[*id*]~]">
<br/>
<table>
<tr>
<td><label style="margin:0.5em"> Email: </label></td>
<td><input type="text" name="mc_EMAIL" size="60"
maxlength="60" style="margin:0.1em" value="" /></td>
</tr>
<tr>
<td><label style="margin:0.5em"> Name: </label></td>
<td><input type="text" name="mc_FNAME" size="60"
maxlength="60" style="margin:0.1em" value="" /></td>
</tr>
<tr>
<td><label style="margin:0.5em"> Last Name: </label></td>
<td><input type="text" name="mc_LNAME" size="60"
maxlength="60" style="margin:0.1em" value="" /></td>
</tr>
<tr>
<td><label style="margin:0.5em"> Website: </label></td>
<td><input type="text" name="mc_URL" size="60" maxlength="60"
style="margin:0.1em" value="" /></td>
</tr>
<tr>
<td><label style="margin:0.5em"> </label></td>
<td><input type="submit" name="subscribe" size="60"
maxlength="60" value="Register" /></td>
</tr>
</table>
</form>
<p><br/></p>
</div>
</html>
Try this:
<form method="post" action="[~[*id*]~]" style="display:inline-block;background-color:#CCDFEF">
Remove the <div style="background-color:#CCDFED"> before the form and </div> after it.