Jquery and Multiple Fields with same Name - html

I have this HTML code :
<table>
<tr>
<td colspan="2">
<input type="radio" name="lineChoice" id="lineChoiceA" value="A"><label for="lineChoiceA">Line A</label>
<input type="radio" name="lineChoice" id="lineChoiceB" value="B"><label for="lineChoiceB">Line B</label>
</td>
</tr>
<tr id="lineA" style="display: none;">
<td>List A : </td>
<td>
<select name="mySelect" id="mySelect">
<option value=""></option>
<option value="A0">A0</option>
<option value="A1">A1</option>
<option value="A2">A2</option>
</select>
</td>
</tr>
<tr id="lineB" style="display: none;">
<td>List B : </td>
<td>
<select name="mySelect" id="mySelect">
<option value=""></option>
<option value="B0">B0</option>
<option value="B1">B1</option>
<option value="B2">B2</option>
</select>
</td>
</tr>
</table>
And I have this JQuery code :
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$(function(){
// If Click on Radio
$('[id^="lineChoice"]').click(function(){
// Hide Line
$("#lineA").hide();
$("#lineB").hide();
// Display Line
if($(this).val()=="A") $("#lineA").show();
if($(this).val()=="B") $("#lineB").show();
});
// If Change on List
$('#mySelect').change(function(){
// Just Display in Console
console.log($(this).val());
});
});
</script>
Both lines are basically hidden.
And by selecting the radio button I display the corresponding line. I voluntarily simplified the name of the lines (it is more complex in my initial code).
If I select row A. Display in JQuery works fine. But I get nothing if I select line B. I know that my "select" have the same name but that's precisely the goal for me. Have only one "select" to process but only the displayed one.
Hoping to be clear in my question which is "Why no display when selecting line B while it works with line A...?

You cannot have two elements with the same id within one page. Hence I update your code using different Ids for the two selects.
In order to register the event using the name, you can use the attribute selector instead. [name="mySelect"] would do the trick.
The reason why the change event is not working on line B is because $('#mySelect').change(function(){}) only register the event on the first select which turn out is the Line A <select>
$(document).ready(() => {
$('input[type="radio"]').on('change', (e) => {
$("#lineA").hide();
$("#lineB").hide();
if (e.currentTarget.id === 'lineChoiceA') {
$("#lineA").show();
} else {
$("#lineB").show();
}
})
// If Change on List
$('[name="mySelect"]').change(function() {
// Just Display in Console
console.log($(this).val());
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td colspan="2">
<input type="radio" name="lineChoice" id="lineChoiceA" value="A"><label for="lineChoiceA">Line A</label>
<input type="radio" name="lineChoice" id="lineChoiceB" value="B"><label for="lineChoiceB">Line B</label>
</td>
</tr>
<tr id="lineA" style="display: none;">
<td>List A : </td>
<td>
<select name="mySelect" id="mySelect1">
<option value=""></option>
<option value="A0">A0</option>
<option value="A1">A1</option>
<option value="A2">A2</option>
</select>
</td>
</tr>
<tr id="lineB" style="display: none;">
<td>List B : </td>
<td>
<select name="mySelect" id="mySelect2">
<option value=""></option>
<option value="B0">B0</option>
<option value="B1">B1</option>
<option value="B2">B2</option>
</select>
</td>
</tr>
</table>

Related

How can I auto select value of dropdown using an ID value?

I am creating a vehicle lookup service for visitors to my employer's website. By entering their vehicle details either manually or by entering their reg, the site products will be filtered to match fitment for that specific vehicle.
If the customer enters their reg and clicks submit, the results are captured in a hidden table on the same page. I want the manual selector values to update according to such hidden values.
How would I get the search data to show in the appropriate selector option? (I understand that none of the results is an option but if it was then how would I do it)
Here is the manual vehicle details code:
<div class="block-content">
<div class="level">
<select class="ymm-select" name="_make">
<option value="">-- Make --</option>
<option value="BMW">BMW</option>
</select>
</div>
<div class="level">
<select class="ymm-select disabled" name="_model" disabled="disabled">
<option value="">-- Model --</option>
</select>
</div>
<div class="level">
<select class="ymm-select disabled" name="_year" disabled="disabled">
<option value="">-- Year --</option>
</select>
</div>
<div class="ymm-clear"> </div>
<div class="ymm-extra" style="display:none">
<div class="ymm-category-container">
<div class="ymm-clear"> </div>
</div>
</div>
<button type="button" title="Search" class="button ymm-submit-any-selection">Search</button>
</div>
Here is the vehicle result data:
<table class="table" hidden="">
<tbody>
<tr>
<td>Make:</td>
<td id="dvla-search-make">FORD </td>
</tr>
<tr>
<td>Model:</td>
<td id="dvla-search-model">FOCUS</td>
</tr>
<tr>
<td>Year:</td>
<td id="dvla-search-year">2009</td>
</tr>
<tr>
<td>Engine:</td>
<td id="dvla-search-engine">1753cc</td>
</tr>
<tr>
<td id="dvla-search-fuel">Fuel Type:</td>
<td>DIESEL</td>
</tr>
</tbody>
</table>
You mean something like this?
window.addEventListener("load",() => { // when the page loads
document.querySelectorAll("[id^=dvla-search]").forEach(entry => { // find all table cells with ids starting with dvla-search
const val = entry.innerText.trim();
const name = entry.id.replace("dvla-search-","_"); // find the name of the
const elem = document.querySelector("[name="+name+"]") // select associated with the cell id
if (elem) { // if found
let vals = [...elem.querySelectorAll("option")].map(opt => opt.value)
if (!vals.includes(val)) { // only add if not already there
const opt = new Option(val,val,true,true); // make an option and select it
elem.appendChild(opt); // add the option
elem.removeAttribute("disabled"); // allow selection
}
else elem.value=val; // IF already there, select it
}
})
});
Here is the manual vehicle details code:
<div class="block-content">
<div class="level">
<select class="ymm-select" name="_make">
<option value="">-- Make --</option>
<option value="FORD">FORD</option>
</select>
</div>
<div class="level">
<select class="ymm-select disabled" name="_model" disabled="disabled">
<option value="">-- Model --</option>
</select>
</div>
<div class="level">
<select class="ymm-select disabled" name="_year" disabled="disabled">
<option value="">-- Year --</option>
</select>
</div>
<div class="ymm-clear"> </div>
<div class="ymm-extra" style="display:none">
<div class="ymm-category-container">
<div class="ymm-clear"> </div>
</div>
</div>
<button type="button" title="Search" class="button ymm-submit-any-selection">Search</button>
</div>
Here is the vehicle result data:
<table class="table" hidden="">
<tbody>
<tr>
<td>Make:</td>
<td id="dvla-search-make">FORD </td>
</tr>
<tr>
<td>Model:</td>
<td id="dvla-search-model">FOCUS</td>
</tr>
<tr>
<td>Year:</td>
<td id="dvla-search-year">2009</td>
</tr>
<tr>
<td>Engine:</td>
<td id="dvla-search-engine">1753cc</td>
</tr>
<tr>
<td id="dvla-search-fuel">Fuel Type:</td>
<td>DIESEL</td>
</tr>
</tbody>
</table>

Pass Textbox value from view to Controller on button click without using form in ASP.NET MVC 4

I have to pass the value entered in my text box to a controller class on click of submit buttton. I do not want to use Form or I do not have a model class for this field. How it can be achieved?
Below is the sample code for your reference
<table style="width:100%;">
<tr>
<td style="text-align:center;font-weight:bold;width:95%;">My header</td>
<td>
<select>
<option value="Choose">Select</option>
<option value="val1">val1</option>
<option value="val2">val2</option>
<option value="val3">val3</option>
</select>
</td>
<td>
<input type ="text" id="searchField" name="searchFieldText">
</td>
<td>
<input type="submit" style="color:black;background-color:rgb(216, 214, 208)" value="Search" onclick="location.href='#Url.Action("ExecuteSearch", "MyController",new { <!--I want to pass the textbox value as param here-->})'" />
</td>
<td style="text-align:right;width:10%;">#Html.ActionLink("LOGOUT", "logout")</td>
</tr>
</table>
Thanks in advance!!!
There are various options to achieve the same. You can take help of Jquery like this:
<a href='#url.action("LOGOUT","logout")?someParameter=$('#searchField').val()' >Text for Link Name</a>
On controller level you have to add string parameter to the logout method. Like:
Public ActionResult Logout(string someParameter)
{
//your code
}
Using AJAX
Below code snippet will work using ajax call
<div class="row">
<table style="width:100%;">
<tr>
<td style="text-align:center;font-weight:bold;width:95%;">My header</td>
<td>
<select>
<option value="Choose">Select</option>
<option value="val1">val1</option>
<option value="val2">val2</option>
<option value="val3">val3</option>
</select>
</td>
<td>
<input type="text" id="searchField" name="searchFieldText"/>
</td>
<td>
<input type="submit" style="color:black;background-color:rgb(216, 214, 208)" value="Search" onclick="submitClick()" />
</td>
<td style="text-align:right;width:10%;">#Html.ActionLink("LOGOUT", "logout")</td>
</tr>
</table>
</div>
<script>
function submitClick()
{
$.ajax({
url: "../MyController/ExecuteSearch?someParameter=" + $('#searchField').val(),
type: "GET",
success: function (data) {
debugger;
// your custom code
}
});
}
</script>
Hope this helps!

Bootstrap Input and Select in a table slightly off

I am using Bootstrap 3 and I have a table with some inputs and selects.
The inputs are about 1px lower than the select:
and no matter what I do I cant seem to get them to line up on the same level.
This rows HTML is like so:
<tr id="other-license-row-new" data-other-license-id="-1">
<td class="text-center">
Attach File
</td>
<td>
<select class="form-control other-license-class">
<option>C - Car</option>
<option>LR - Light Rigid</option>
<option>MR - Medium Rigid</option>
<option>HR - Heavy Rigid</option>
<option>HC - Heavy Combination</option>
<option>MC - Multi Combination</option>
</select>
</td>
<td class="expiry-date-row">
<input type="text" class="form-control other-license-expiryDate date-picker" data-position="top left">
</td>
<td>
<select type="text" class="other-license-state form-control">
<option value="NSW">NSW</option>
<option value="QLD">QLD</option>
<option value="VIC">VIC</option>
<option value="ACT">ACT</option>
<option value="SA">SA</option>
<option value="WA">WA</option>
<option value="NT">NT</option>
<option value="TAS">TAS</option>
</select>
</td>
<td>
<input type="text" class="other-license-number form-control">
</td>
<td class="action-column">
...dropdown button html removed for brevity...
</td>
</tr>
I would like to know how I can align the inputs and select so it doesnt have he 1px difference.
I have tried:
select, input{
vertical-align: middle; //as well as all options in web inspector
}

Mailto:subject age old query

I have a question and I think I know the answer.
Have some html code where im using the action="mailto: function.
My question is can I assign the Subject to the generated email from the selection in the code below? E.g. Subject: Urgent; when urgent is selected.
<head>
<title></title>
</head>
<body>
<form enctype="text/plain" method="post" id="orderform" action="mailto:randomeemail#gmail.com?subject" >
<table border="1" align="left" style="width: 100%">
<tbody>`enter code here`
<tr>
<td style="text-align: justify;"><label for="element_10" class="Deliverys">What
Type of Delivery is required?</label></td>
<td style="text-align: justify;">
<select name="Urgency Required">
<option value="Standard">Standard Delivery</option>
<option value="Fast">Fast</option>
<option value="Urgent">Urgent</option>
<option value="Required in 4 hours">Required in 4 hours</option>
<option value="Required in 2 hours">Required in 2 hours</option>
<option value="Pick-up">Pick-up</option>
</select>
</td>
</tr>
<input type="submit" value="Submit" name="Submit" />
</form>
</body>
</html>
If you don't mind using Javascript, you can do the following:
<script>
function myFunction(){
window.location.href = 'mailto:randomeemail#gmail.com?subject=' + document.querySelector('select[name="Urgency Required"]').value
return false
}
</script>
<form enctype="text/plain" method="post" id="orderform" onsubmit="return myFunction();" >
<table border="1" align="left" style="width: 100%">
<tbody>`enter code here`
<tr>
<td style="text-align: justify;"><label for="element_10" class="Deliverys">What
Type of Delivery is required?</label></td>
<td style="text-align: justify;">
<select name="Urgency Required">
<option value="Standard">Standard Delivery</option>
<option value="Fast">Fast</option>
<option value="Urgent">Urgent</option>
<option value="Required in 4 hours">Required in 4 hours</option>
<option value="Required in 2 hours">Required in 2 hours</option>
<option value="Pick-up">Pick-up</option>
</select>
</td>
</tr>
<input type="submit" value="Submit" name="Submit"></input>
</tbody>
</table>
</form>
Bascially, when the form submits, it'll dynamically generate the mailto link, and open it. It then returns false, which stops the form from actually submitting (i.e. redirecting to the action).

easy and quick Simple form edit please

The forms which l have here, have required files to be entered before the submit button. All l need it for the top 'title' list to be required='true'. I know how to do it for each and every of the text forms but unsure how to position the code for an options choice list.
Cheers guys.
Please take a look at the jsfiddle l have created for this:
http://jsfiddle.net/New_to_Websites/5A4vS/#run
Code:
<script language="javascript">
var sa_sent_text = 'Thank you for contacting us. We will get back to you soon.';
function sendme() {
alert(sa_sent_text);
document.getElementById("name").value = "";
document.getElementById("message").value = "";
document.getElementById("subject").value = "";
document.getElementById("email").value = "";
return false;
}
</script>
<div id="sa_contactdiv">
<form name=sa_htmlform style="margin:0px" onsubmit="return sendme();">
<table>
<tr>
<td>Title:
<br>
<select name="title" size="1">
<option value="Mr.">Mr.</option>
<option value="Mrs.">Mrs.</option>
<option value="Miss">Miss</option>
<option value="Ms.">Ms.</option>
<option value="Dr.">Dr.</option>
<option value="Prof.">Prof.</option>
<option value="Other">Other</option>
</select>
</td>
</tr>
<tr>
<td>Name:
<br>
<input type="text" id="name" name="name" />
</td>
</tr>
<tr>
<td>E-mail Address: <span style="color:#D70000">*</span>
<br>
<input type="text" id="email" name="email" required="true" />
</td>
</tr>
<tr>
<td>Subject: <span style="color:#D70000">*</span>
<br>
<input type="text" id="subject" name="subject" required="true" />
</td>
</tr>
<tr>
<td>Message: <span style="color:#D70000">*</span>
<br>
<textarea name="message" id="message" cols="42" rows="9" required="true"> </textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Send Message" style="font-weight:bold">
</td>
</tr>
</table>
</form>
</div>
This should work:
<select name="title" size="1" required aria-required=”true”>
<option value="">Please Choose</option>
<option value="Mr.">Mr.</option>
<option value="Mrs.">Mrs.</option>
<option value="Miss">Miss</option>
<option value="Ms.">Ms.</option>
<option value="Dr.">Dr.</option>
<option value="Prof.">Prof.</option>
<option value="Other">Other</option>
</select>
If you have an option with a empty value as default, the required attribute works.
Working Fiddle
The required attribute is a boolean attribute. When specified, the user will be required to select a value before submitting the form.
If a select element has a required attribute specified, does not have a multiple attribute specified, and has a display size of 1; and if the value of the first option element in the select element's list of options (if any) is the empty string, and that option element's parent node is the select element (and not an optgroup element), then that option is the select element's placeholder label option.
Quoted from http://dev.w3.org/html5/spec-author-view/the-select-element.html
Try this
<td>Title:<span style="color:#D70000">*</span>
<br>
<select name="title" size="1" required="true">
<option value=""></option>
<option value="Mr.">Mr.</option>
<option value="Mrs.">Mrs.</option>
<option value="Miss">Miss</option>
<option value="Ms.">Ms.</option>
<option value="Dr.">Dr.</option>
<option value="Prof.">Prof.</option>
<option value="Other">Other</option>
</select>