.JS form not working on Safari - javascript
I have a form in an iframe that submits information to an MLS IDX and shows the properties. The form works great on Chrome and Mozilla, but when using Safari it just reloads the page in a new window instead of showing the results page. I copied the .js and form from the IDX website and do not know anything about .js. The page actually targets a remote .js file and also has .js on the actual page. The page is located at mariaraymer.com/test1.html
Any help is appreciated.
Thank you!
Update 2: Noticed in Safari quickly goes to second page after submit then returns to original URL - Url of page is http://mlssearch.topproduceridx.com/system/safari.asp?url=. Page has this javscript. Page looks to be dedicated to safari as it does not go to a page like this for chrome or firefox.
<script language="JavaScript">
window.setTimeout(function() {
if (document.cookie.indexOf('test_cookie=1') < 0) {
var name = 'test_cookie', div = document.getElementById(name), iframe = document.createElement('iframe'), form = document.createElement('form');
iframe.name = name;
iframe.src = 'javascript:false';
div.appendChild(iframe);
form.action = location.toString();
form.method = 'POST';
form.target = "_parent";
div.appendChild(form);
form.submit();
}
else {
if (location.search.indexOf('https://') > 0)
top.location.href = location.search.replace('?url=https://','http://');
else if (location.search.indexOf('http://') > 0)
top.location.href = location.search.replace('?url=http://','http://');
}
},
10);
</script>
Update: Error in Chrome Console
Uncaught ReferenceError: SwapPriceRange is not defined
3extensions::uncaught_exception_handler:8 Error in event handler for (unknown): TypeError: Cannot read property 'state' of null
at CSRecorder.onQueryStateCompleted (chrome-extension://cplklnmnlbnpmjogncfgfijoopmnlemp/content_scripts/recorder.js:43:13)
Moved Code around and received this error:
extensions::uncaught_exception_handler:8 Error in event handler for (unknown): TypeError: Cannot read property 'state' of null
at CSRecorder.onQueryStateCompleted (chrome-extension://cplklnmnlbnpmjogncfgfijoopmnlemp/content_scripts/recorder.js:43:13)
test1.html:1 Resource interpreted as Image but transferred with MIME type text/html: "http://www.mariaraymer.com/".
extensions::uncaught_exception_handler:8 Error in event handler for (unknown): TypeError: Cannot read property 'state' of null
at CSRecorder.onQueryStateCompleted (chrome-extension://cplklnmnlbnpmjogncfgfijoopmnlemp/content_scripts/recorder.js:43:13)
Here is the remote .js code:
//***************THE FOLLOWING FUNCTIONS ARE USED THROUGHOUT************
function openWin(url,windowName,options){
var WindowHandle=window.open(url,windowName,options);
WindowHandle.focus();
}
function openWinFormSubmit(frm,frmaction,url,windowName,options){
frm.submit();
var WindowHandle = window.open(url,windowName,options);
document.location = frmaction;
WindowHandle.focus();
}
var ns6=document.getElementById&&!document.all
var ie=document.all
function showSpan(spanID,thetext){
if (ie) eval("document.all."+spanID).innerHTML=thetext
else if (ns6) document.getElementById(spanID).innerHTML=thetext
}
function hideSpan(spanID){
if (ie) eval("document.all."+spanID).innerHTML=' '
else if (ns6) document.getElementById(spanID).innerHTML=' '
}
function getElementBy(elemTag){
var elem = document.getElementById (elemTag);
if (elem)
return elem;
var elems = document.getElementsByName (elemTag);
if (elems.length > 0)
return elems[0];
return null;
}
function toggleOpenCloseElem(elemName){
var elem = getElementBy (elemName);
if (!elem)
return;
if (elem.style.display == "")
elem.style.display = "none";
else
elem.style.display = "";
}
function toggleOpenElem(elemName){
var elem = getElementBy (elemName);
if (!elem)
return;
else
elem.style.display = "";
}
function toggleCloseElem(elemName){
var elem = getElementBy (elemName);
if (!elem)
return;
else
elem.style.display = "none";
}
function GetCookie(name) { // use: GetCookie("name");
var thecookie = document.cookie;
var index = thecookie.indexOf(name + "=");
if (index == -1) return null;
index = thecookie.indexOf("=", index) + 1; // first character
var endstr = thecookie.indexOf(";", index);
if (endstr == -1) endstr = thecookie.length; // last character
return unescape(thecookie.substring(index, endstr));
}
function SetCookie(name, value) { // use: SetCookie("name", value);
document.cookie=name + "=" + escape(value) + ";";
}
function testForObject(Id){
var o = document.getElementById(Id);
if (o){return true;}return false;
}
function setAdminSection(str){
if(str.length >= 14){
document.getElementById('AdminSection').style.fontSize = '28px'
}else{
document.getElementById('AdminSection').style.fontSize = '36px'
}
document.getElementById('AdminSection').innerHTML=str;
}
//***************THE ABOVE FUNCTION IS USED THROUGHOUT************
//***************THE FOLLOWING FUNCTION IS USED FOR FORM VALIDATION************
function confirmPrompt(msg,url){
if(confirm(msg))
document.location = url;
}
function confirmPromptFormSubmit(msg,frm,url){
if(confirm(msg)){
frm.submit();
document.location = url;
}
}
function validateLength(objTB,maxChar){
if (objTB.value.length > maxChar){return false;}
return true;
}
function validateEmail(objTB){
var objRegExp = /^[a-z0-9]([a-z0-9_\-\.]*)#([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
return objRegExp.test(objTB.value);
}
function validateEmailNonReq(objTB){
var invalidChars = "*|,\":<> []{}`\';()&$#%";
if (objTB.value.length > 0){
if (objTB.value.indexOf('#') < 0 || objTB.value.indexOf('.') < 0 || objTB.value.length < 5){return false;}
for (var i = 0; i < objTB.value.length; i++){
if (invalidChars.indexOf(objTB.value.charAt(i)) != -1){return false;}
}
return true;
}
return true;
}
function validateTextBox(objTB){
if (objTB.value==''){return false;}
return true;
}
function validateSelectList(objTB){
if (objTB.selectedIndex==''){return false;}
return true;
}
function validateNumberTextBoxNonReq(objTB){
if (isNaN(objTB.value)){return false;}
return true;
}
function validateNumberTextBox(objTB){
if (objTB.value=='' || isNaN(objTB.value)){return false;}
return true;
}
function validateZipCodeTextBoxNonReq(objTB){
reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
if (!reZip.test(objTB.value)){return false;}
return true;
}
//***************THE ABOVE FUNCTION IS USED FOR FORM VALIDATION************
//***************THE FOLLOWING FUNCTIONS ARE USED BY THE PROPERTY COMPARE FEATURE************
function commitCompareList(){
if(GLOBAL_CompareList != '') {SetCookie("VAR_CompareList",GLOBAL_CompareList);}
if(GLOBAL_CompareCount != '') {SetCookie("CompareCount",GLOBAL_CompareCount);}
}
function sendToCompare(querystring){
if (GLOBAL_CompareCount < 2){
alert('Please check more than 1 listing!');
}else{
if (GLOBAL_CompareCount > 4){
alert('Please check no more than 4 listings!');
}else{
window.location = "/property/propertycompare.asp? VAR_CompareList=" + GLOBAL_CompareList + "&CompareCount=" + GLOBAL_CompareCount + querystring;
}
}
}
function compareClear(){
GLOBAL_CompareList = '';
GLOBAL_CompareCount = '';
if(GetCookie('VAR_CompareList') != null && GetCookie('VAR_CompareList') != ""){SetCookie("VAR_CompareList",'');}
if(GetCookie('CompareCount') != null && GetCookie('CompareCount') != ""){SetCookie("CompareCount",'');}
}
function addCompare(obj){
//the maximum number of listings a user can check to compare
var Max_Number_Properties = 4;
if(obj.checked == true){
if(GLOBAL_CompareCount == Max_Number_Properties){
obj.checked = false;
alert('You have already chosen ' + Max_Number_Properties + ' properties!');
}else{
GLOBAL_CompareList = GLOBAL_CompareList + obj.value;
GLOBAL_CompareCount = Math.round(GLOBAL_CompareCount) + 1;
}
}else{
GLOBAL_CompareCount = Math.round(GLOBAL_CompareCount) - 1;
GLOBAL_CompareList = GLOBAL_CompareList.replace(obj.value,"");
}
}
//***************THE ABOVE FUNCTIONS ARE USED BY THE COMPARE TOOL************
//***************THE FOLLOWING FUNCTIONS ARE USED BY THE eNavigator FEATURE************
//Clears eNavigator Cookies
function eNavigatorClear(){
if(GetCookie('eNavigator_SearchForm_State') != null && GetCookie('eNavigator_SearchForm_State') != ""){SetCookie('eNavigator_SearchForm_State','')};
if(GetCookie('eNavigator_Prev_CenterLat') != null && GetCookie('eNavigator_Prev_CenterLat') != ""){SetCookie('eNavigator_Prev_CenterLat','')};
if(GetCookie('eNavigator_Prev_CenterLng') != null && GetCookie('eNavigator_Prev_CenterLng') != ""){SetCookie('eNavigator_Prev_CenterLng','')};
if(GetCookie('eNavigator_Prev_ZoomLevel') != null && GetCookie('eNavigator_Prev_ZoomLevel') != ""){SetCookie('eNavigator_Prev_ZoomLevel','')};
if(GetCookie('eNavigator_Prev_MapStyle') != null && GetCookie('eNavigator_Prev_MapStyle') != ""){SetCookie('eNavigator_Prev_MapStyle','')};
if(GetCookie('eNavigator_VAR_City') != null && GetCookie('eNavigator_VAR_City') != ""){SetCookie('eNavigator_VAR_City','')};
if(GetCookie('eNavigator_VAR_ZipCode') != null && GetCookie('eNavigator_VAR_ZipCode') != ""){SetCookie('eNavigator_VAR_ZipCode','')};
if(GetCookie('eNavigator_PRM_PropertyTypeCode') != null && GetCookie('eNavigator_PRM_PropertyTypeCode') != ""){SetCookie('eNavigator_PRM_PropertyTypeCode','')};
if(GetCookie('eNavigator_PRM_Minimum_Price') != null && GetCookie('eNavigator_PRM_Minimum_Price') != ""){SetCookie('eNavigator_PRM_Minimum_Price','')};
if(GetCookie('eNavigator_PRM_Maximum_Price') != null && GetCookie('eNavigator_PRM_Maximum_Price') != ""){SetCookie('eNavigator_PRM_Maximum_Price','')};
if(GetCookie('eNavigator_PRM_Minimum_Beds') != null && GetCookie('eNavigator_PRM_Minimum_Beds') != ""){SetCookie('eNavigator_PRM_Minimum_Beds','')}; //KILLS BATHS TOO (don't ask)
}
//***************THE ABOVE FUNCTIONS ARE USED BY eNavigator************
This is the .js on the actual page with the form
<script type="text/javascript">
function validateForm1(frm) {
var msg = '';
if (!validateTextBox(frm.VAR_MLSNumber)) { msg = msg + 'One or More MLS Numbers\n'; }
if (msg == '') {
return true;
}
else {
msg = 'The following field(s) are required:\n\n' + msg + '\n';
alert(msg);
return false;
}
}
function validateForm2(frm) {
var msg = '';
var CityCnt = 0;
var ZipCnt = 0;
for (var x = 0; x < frm.VAR_City.length; x++) {
if (frm.VAR_City.options[x].selected) {
CityCnt = CityCnt + 1;
}
}
for (var x = 0; x < frm.VAR_ZipCode.length; x++) {
if (frm.VAR_ZipCode.options[x].selected) {
ZipCnt = ZipCnt + 1;
}
}
if (CityCnt > 10) { msg = msg + '10 or less Cities\n'; }
if (ZipCnt > 10) { msg = msg + '10 or less Zip Codes\n'; }
if (frm.VAR_City.selectedIndex < 0 && frm.VAR_ZipCode.selectedIndex < 0) { msg = msg + 'One or more Cities OR Zip Codes\n'; }
if (msg == '') {
toggleOpenCloseElem('searchingTR1');
return true;
} else {
msg = 'The following field(s) are required:\n\n' + msg + '\n';
alert(msg);
return false;
}
}
function disableFields(selVal) {
if (selVal == 'COM' || selVal == 'CML' || selVal == 'LND' || selVal == 'IMF' || selVal == 'RSL') {
if (testForObject('PRM_Minimum_Beds')) {
frmMain2.PRM_Minimum_Beds.selectedIndex = -1;
frmMain2.PRM_Minimum_Beds.disabled = true;
}
if (testForObject('PRM_Minimum_baths')) {
frmMain2.PRM_Minimum_baths.selectedIndex = -1;
frmMain2.PRM_Minimum_baths.disabled = true;
}
} else {
if (testForObject('PRM_Minimum_Beds')) {
if (frmMain2.PRM_Minimum_Beds.selectedIndex < 0) {
frmMain2.PRM_Minimum_Beds.selectedIndex = 0;
}
frmMain2.PRM_Minimum_Beds.disabled = false;
}
if (testForObject('PRM_Minimum_baths')) {
if (frmMain2.PRM_Minimum_baths.selectedIndex < 0) {
frmMain2.PRM_Minimum_baths.selectedIndex = 0;
}
frmMain2.PRM_Minimum_baths.disabled = false;
}
}
if (testForObject('PRM_Custom9')) {
if (selVal != 'IMF' && selVal != 'RNT') {
frmMain2.PRM_Custom9.selectedIndex = -1;
frmMain2.PRM_Custom9.disabled = true;
} else {
frmMain2.PRM_Custom9.selectedIndex = 0;
frmMain2.PRM_Custom9.disabled = false;
}
}
if (testForObject('PRM_Custom12')) {
if (selVal == 'COM' || selVal == '') {
frmMain2.PRM_Custom12.disabled = false;
} else {
frmMain2.PRM_Custom12.checked = false;
frmMain2.PRM_Custom12.disabled = true;
}
}
}
</script>
This is the form
<table cellpadding=0 cellspacing=0 border=0 class="tableBorder" width=230px>
<tr>
</tr>
<tr>
<td valign=top colspan=0 style="padding:0px; background-color:#FFFFFF;">
<table border=0 cellpadding=0 cellspacing=0 width=67%>
<tr>
<!--td style="border-bottom: solid 1px #000000;"><img src="/images/spacer.gif" width=2></td>
<td class=searchTab align=center><div onClick="document.location.href='http://mlssearch.topproduceridx.com/property/map.asp';">Map</div></td-->
</tr>
<tr>
<td colspan=0 bgcolor="ffffff">
<table border=0 cellpadding=0 cellspacing=2 class="Website_Text" width=100%>
<form method="get" action="http://mlssearch.topproduceridx.com/property/proplist.asp" name="frmMain2" target="_blank" onSubmit="return(validateForm2(this));">
<input type="hidden" name="VAR_ShowResultOptions" value="1">
<input type="hidden" name="VAR_SearchSource" value="propsearch">
<tr>
<td class="searchPageSectionHeader">Listing Location <font color=FF0000>*</font></td>
</tr>
<tr>
<td valign=top align=left class="searchPageSectionBG" style="padding-top:8px" nowrap>MLS Number:</td>
</tr>
<tr>
<td><input name="VAR_MLSNumber" size=30 maxlength=38 value=""></td></tr>
<tr class="searchPageSectionBG">
<td nowrap style="padding-top:8px" >Street Address: </td>
</tr>
<tr class="searchPageSectionBG">
<td width=100%>
<input name="PRM_Address" size=30 maxlength=32 value=""><br>
</td>
</tr>
<tr>
<td>
<table border=0 cellpadding=2 cellspacing=2 class="Website_Text">
</table>
</td>
</tr>
<tr>
<td class="searchPageSectionHeader">Listing Features</td>
</tr>
<tr>
<td>
<table border=0 cellpadding=2 cellspacing=0 class="Website_Text" width=80%>
<tr class="searchPageSectionBG">
<td> Type:</td>
<td colspan=0 width=15px>
<select name="PRM_PropertyTypeCode" onChange="javascript:disableFields(this.value); SwapPriceRange(this.value);" class="searchPageSectionBG">
<option value="">Any listing type</option>
<option value="RES">Single Family Home</option>
<option value="CND">Townhome or Condo</option>
<option value="IMF">Multi-Family Home</option>
<option value="LND">Lots and Land</option>
<option value="COM">Commercial</option>
<option value="FRM">Farm</option>
</select>
</td>
</tr>
<tr class="searchPageSectionBG">
</tr>
<tr class="searchPageSectionBG">
<td nowrap> Price:</td>
<td align="left">
<select name="PRM_Minimum_Price" id="PRM_Minimum_Price" class="searchPageSectionBG">
<option value="">Any price</option>
<option value="50000">$50,000</option>
<option value="100000">$100,000</option>
<option value="150000">$150,000</option>
<option value="200000">$200,000</option>
<option value="250000">$250,000</option>
<option value="300000">$300,000</option>
<option value="350000">$350,000</option>
<option value="400000">$400,000</option>
<option value="450000">$450,000</option>
<option value="500000">$500,000</option>
<option value="550000">$550,000</option>
<option value="600000">$600,000</option>
<option value="650000">$650,000</option>
<option value="700000">$700,000</option>
<option value="750000">$750,000</option>
<option value="800000">$800,000</option>
<option value="850000">$850,000</option>
<option value="900000">$900,000</option>
<option value="950000">$950,000</option>
<option value="1000000">$1,000,000</option>
<option value="1250000">$1,250,000</option>
<option value="1500000">$1,500,000</option>
<option value="1750000">$1,750,000</option>
<option value="2000000">$2,000,000</option>
<option value="">Any price</option>
</select></td></tr>
<tr class="searchPageSectionBG"><td align="center">
to</td>
<td align="left">
<select name="PRM_Maximum_Price" id="PRM_Maximum_Price" class="searchPageSectionBG">
<option value="">Any price</option>
<option value="50000">$50,000</option>
<option value="100000">$100,000</option>
<option value="150000">$150,000</option>
<option value="200000">$200,000</option>
<option value="250000">$250,000</option>
<option value="300000">$300,000</option>
<option value="350000">$350,000</option>
<option value="400000">$400,000</option>
<option value="450000">$450,000</option>
<option value="500000">$500,000</option>
<option value="550000">$550,000</option>
<option value="600000">$600,000</option>
<option value="650000">$650,000</option>
<option value="700000">$700,000</option>
<option value="750000">$750,000</option>
<option value="800000">$800,000</option>
<option value="850000">$850,000</option>
<option value="900000">$900,000</option>
<option value="950000">$950,000</option>
<option value="1000000">$1,000,000</option>
<option value="1250000">$1,250,000</option>
<option value="1500000">$1,500,000</option>
<option value="1750000">$1,750,000</option>
<option value="2000000">$2,000,000</option>
<option value="">Any price</option>
</select>
</td>
</tr>
<tr class="searchPageSectionBG">
<td nowrap> Bedrooms:</td>
<td align="left" >
<select name="PRM_Minimum_Beds" id="PRM_Minimum_Beds" class="searchPageSectionBG">
<option value="" >Any number</option>
<option value="1">At least 1</option>
<option value="2">At least 2</option>
<option value="3">At least 3</option>
<option value="4">At least 4</option>
</select>
</td>
</tr>
<tr class="searchPageSectionBG">
<td nowrap> Bathrooms:</td>
<td align="left">
<select name="PRM_Minimum_baths" id="PRM_Minimum_baths" class="searchPageSectionBG">
<option value="" >Any number</option>
<option value="1">At least 1</option>
<option value="2">At least 2</option>
<option value="3">At least 3</option>
<option value="4">At least 4</option>
</select>
</td>
</tr>
</table>
</td>
<tr>
<td>
<table border=0 cellpadding=0 cellspacing=0 class="Website_Text" width=100%>
</table>
</td>
</tr>
<tr>
<td class="searchPageSectionHeader">Lot Features</td>
</tr>
<tr>
<td>
<table border=0 cellpadding=0 cellspacing=0 class="Website_Text" width=100%>
<tr class="searchPageSectionBG">
<td nowrap> Lot Size (Acres):</td>
<td width=100% color="#7b8283">
<select name="VAR_SqFt" class="searchPageSectionBG">
<option value="">Any size</option>
<option value="0-.49">Less than 0.5</option>
<option value=".5-1">0.5 - 1</option>
<option value="1-2">1 - 2</option>
<option value="2-5">2 - 5</option>
<option value="5-10">5 - 10</option>
<option value="10.1-1000000">More than 10</option>
</select>
</td>
</tr>
<tr class="searchPageSectionBG">
<td colspan=0 nowrap>
<table border=0 cellpadding=0 cellspacing=0 class="Website_Text">
<tr>
<td class="searchPageSectionBG"><input type=checkbox value=1 name="PRM_Waterfront" > Waterfront </td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan=0 valign=middle nowrap align=left >
<input type="submit" align="left" value="Search" style="background-color: #ec894d; color: #ffffff; border-color: #ec894d; vertical-align: middle; text-align: center;" class="button">
<input type=radio name=PRM_OrderBy style="display:none;" value="pReciprocity ASC, pPrice DESC" Checked> </td>
</tr>
</table>
<p>
</td>
</tr>
</form>
</table>
Your HTML is invalid. You can't have <form> as a child of <table> The children of <table> must be table elements like <thead>, <tbody>, <tfoot>, or <tr>. When you give invalid HTML like this, the browser tries to fix it up, and different browsers do this differently. What's happening is that the submit button is not connected to the form properly, so it's not sending to the action URL.
Instead of putting the form inside the table, put the table inside the form:
<form method="get" action="http://mlssearch.topproduceridx.com/property/proplist.asp" name="frmMain2" target="_blank" onSubmit="return(validateForm2(this));">
<input type="hidden" name="VAR_ShowResultOptions" value="1">
<input type="hidden" name="VAR_SearchSource" value="propsearch">
<table border=0 cellpadding=0 cellspacing=2 class="Website_Text" width=100%>
...
</table>
</form>
Also, your <table> and </table> tags aren't matched up properly: you have 8 <table> and 9 </table>.
Related
Filter table with logical operations using Jquery
Trying to implement table filter with And/Or logical operations. Now the table is filtering based on individual datepicker and select field. If I select AND the table should show both datepicker and select field results, if I select OR either of one should display. Is it possible to achieve it in jquery. Any help is appreciated. $(document).on("change", "#datepicker .created_on , select", function() { var dataVal = $("#datepicker .created_on").datepicker('getDate'); dataVal = $.datepicker.formatDate("d/m/y", dataVal); var select_value = $("#nameselect select").val(); var logic = $("select.logic").val(); $("#myTable tr:not('#table-header')").hide(); if (select_value != "" && dataVal != "") { $("#myTable tbody > tr").each(function() { var data_type = $(this).data('type'); var datess = $(this).find("td:eq(3) label").text() var condition = (logic == "1") ? ((data_type == select_value) && (datess == dataVal)) : ((data_type == select_value) || (datess == dataVal)) if (condition) { $(this).show(); } }); } else if (dataVal != "" && select_value == "") { console.log("dwd") $("label:contains('" + dataVal + "')").each(function() { $(this).closest('tr').show(); }); } else if (dataVal == "" && select_value != "") { $("[data-type=" + select_value + "]").each(function() { $(this).show(); }); } }); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script> <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" /> <div style="display:inline-flex;"> <label>logic</label> <select style="width:60px;" required="" class="logic"> <option value="">select</option> <option value="1">OR</option> <option value="2">AND</option> </select> </div> <div style="margin-left:10px;display:inline-flex;" id="nameselect"> <label for="value">Name</label> <select style="width:120px;" id="mylist" class='form-control' required=""> <option value="">select</option> <option value="Mary">Mary</option> <option value="John">John</option> <option value="Martin">Martin</option> <option value="Rozi">Rozi</option> </select> </div> <table class="" cellpadding="1" cellspacing="1" style="margin-top:10px;" id="myTable"> <thead> <tr id="table-header"> <th><label> NAME</label></th> <th><label> TYPE</label></th> <th><label>Doc</label></th> <th style="margin-left:10px"><label>Date</label></th> </tr> </thead> <tbody id=""> <tr class="" data-type="Mary"> <td><label>Mary</label></td> <td><label>text</label></td> <td><label>word</label></td> <td><label style="margin-left:10px">1/1/21</label></td> </tr> <tr class="" data-type="John"> <td><label>John</label></td> <td><label>text</label></td> <td><label>word</label></td> <td><label style="margin-left:10px">31/1/21</label></td> </tr> <tr class="" data-type="Martin"> <td><label>Martin</label></td> <td><label>text</label></td> <td><label>word</label></td> <td><label style="margin-left:10px">21/2/21</label></td> </tr> <tr class="" data-type="Rozi"> <td><label>Rozi</label></td> <td><label>text</label></td> <td><label>word</label></td> <td><label style="margin-left:10px">5/10/20</label></td> </tr> </tbody> </table>
You can use check if the logic select value is 1 or 2 depending on this use && or || operator and show required trs . Demo code : $(document).on("change", "#datepicker .created_on , select", function() { var dataVal = $("#datepicker .created_on").datepicker('getDate'); dataVal = $.datepicker.formatDate("d/m/y", dataVal); var select_value = $("#nameselect select").val(); //get select value var logic = $("select.logic").val(); //check if both are not both empty if (select_value != "" && dataVal != "") { $("#myTable tr:not('#table-header')").hide(); $("#myTable tbody > tr").each(function() { var data_type = $(this).data('type'); //get data attr value var datess = $(this).find("td:eq(3) label").text() //get date value //compare var condition = (logic == "1") ? ((data_type == select_value) && (datess == dataVal)) : ((data_type == select_value) || (datess == dataVal)) if (condition) { $(this).show(); //show } }); } else { //show all.. $("#myTable tr").show(); //or some other logic you need . } }); (function($) { $('.datepicker').each(function() { $(this).datepicker({ changeMonth: true, changeYear: true, dateFormat: 'dd-mm-yy', onClose: function() { //triggerFocus(); } }); }); }(jQuery)); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script> <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" /> <div class="datepicker1" style="display:inline-flex;" id="datepicker"> <label for="datepicker">Date</label> <input type="text" style="width:120px;" class="created_on required datepicker" placeholder="DD-MM-YYYY" required=""> </div> <div style="display:inline-flex;"> <label>logic</label> <select style="width:60px;" required="" class="logic"> <option value="">select</option> <option value="1">AND</option> <option value="2">OR</option> </select> </div> <div style="margin-left:10px;display:inline-flex;" id="nameselect"> <label for="value">Name</label> <select style="width:120px;" id="mylist" class='form-control' required=""> <option value="">select</option> <option value="Mary">Mary</option> <option value="John">John</option> <option value="Martin">Martin</option> <option value="Rozi">Rozi</option> </select> </div> <table class="" cellpadding="1" cellspacing="1" style="margin-top:10px;" id="myTable"> <thead> <tr id="table-header"> <th><label> NAME</label></th> <th><label> TYPE</label></th> <th><label>Doc</label></th> <th style="margin-left:10px"><label>Date</label></th> </tr> </thead> <tbody id=""> <tr class="" data-type="Mary"> <td><label>Mary</label></td> <td><label>text</label></td> <td><label>word</label></td> <td><label style="margin-left:10px">1/1/21</label></td> </tr> <tr class="" data-type="John"> <td><label>John</label></td> <td><label>text</label></td> <td><label>word</label></td> <td><label style="margin-left:10px">31/1/21</label></td> </tr> <tr class="" data-type="Martin"> <td><label>Martin</label></td> <td><label>text</label></td> <td><label>word</label></td> <td><label style="margin-left:10px">21/2/21</label></td> </tr> <tr class="" data-type="Rozi"> <td><label>Rozi</label></td> <td><label>text</label></td> <td><label>word</label></td> <td><label style="margin-left:10px">5/10/20</label></td> </tr> </tbody> </table>
Jquery dropdown filter for tables
I'm using a code that I also found here that filters a table based on a dropdown selection. I'm not well-versed with JQuery/Javascript. My Question is how can I "show all" the data using this code snippet: dropdown: Show All Done On-going Not yet started $(document).ready(function($) { $('#selectField').change(function() { var selection = $(this).val(); var dataset = $('table').find('tr'); dataset.show(); dataset.filter(function(index, item) { return $(item).find('td:nth-child(1)').text().split(',').indexOf(selection) === -1; }).hide(); }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <select id='selectField'> <option value=" "> Show All </option> <option value="Done"> Done </option> <option value="On-going"> On-going</option> <option value="Not yet started"> Not yet started </option> </select> <table border="2"> <tr> <td>Done</td> </tr> <tr> <td>On-going</td> </tr> <tr> <td>Done</td> </tr> <tr> <td>Not yet started</td> </tr> <tr> <td>Not yet started</td> </tr> </table>
You can check the value of dropdown and then filter it like $(document).ready(function($) { $('#selectField').change(function() { var selection = $(this).val(); var dataset = $('table').find('tr'); var unSelectedData; dataset.show(); if (selection !== ' ') { var unSelectedData = dataset.filter(function(index, item) { return $(item).find('td:nth-child(1)').text().split(',').indexOf(selection) === -1; }) } if (unSelectedData) { unSelectedData.hide(); } }); }); $(document).ready(function($) { $('#selectField').change(function() { var selection = $(this).val(); var dataset = $('table').find('tr'); var unSelectedData; dataset.show(); if (selection !== ' ') { var unSelectedData= dataset.filter(function(index, item) { return $(item).find('td:nth-child(1)').text().split(',').indexOf(selection) === -1; }) } if (unSelectedData) { unSelectedData.hide(); } }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <select id='selectField'> <option value=" "> Show All </option> <option value="Done"> Done </option> <option value="On-going"> On-going</option> <option value="Not yet started"> Not yet started </option> </select> <table border="2"> <tr> <td>Done</td> </tr> <tr> <td>On-going</td> </tr> <tr> <td>Done</td> </tr> <tr> <td>Not yet started</td> </tr> <tr> <td>Not yet started</td> </tr> </table>
The solution is to avoid search action when selection is same value as "Show all" option which is " " I've updated the jQuery code as below, and it works : $(document).ready(function($) { $('#selectField').change(function() { var selection = $(this).val(); var dataset = $('table').find('tr'); dataset.show(); dataset.filter(function(index, item) { if (selection !== ' ') { return $(item).find('td:nth-child(1)').text().split(',').indexOf(selection) === -1; } }).hide(); }); });
how to get the second value from select option in javascript
How do I get the second value of "investortype" and show it within "exploremax"? <script type="text/javascript"> function $(id) { return document.getElementById(id); } function addCommas(nStr) // adds commas for long numbers in function explore().. eg. 123456 = 123,456 { nStr += ''; x = nStr.split('.'); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; } function explore() { // this function will calculate how much credits_all and credits_2_all it costs to explore "total". In addition to that I’d also like it to show exploremax.. but it doesn’t work. var value = document.getElementById(investortype).value; var split = value.split(":"); var v1 = split[0]; var v2 = split[1]; var toexplore = Number($("toexplore").value); var total = Number($("total").value); var exploremax = Math.round(((total * 0.12 * v2) * 1) / 1); var credit = Math.round(((total * 0.51 * v1) + 700) * Math.pow(10, 10)) / Math.pow(10, 10); var credit_2 = Math.round(((total * 0.51 * v1) + 850) * Math.pow(10, 10)) / Math.pow(10, 10); var credit_all = addCommas(Math.round((toexplore * credit) * 1) / 1); var credit_2_all = addCommas(Math.round((toexplore * credit_2_all) * 1) / 1); var show_exploremax = addCommas(exploremax); if ((toexplore == "" || toexplore == null) && (total == "" || total == null)) { $("credit_all").innerHTML = "Error"; $("credit_2_all").innerHTML = "Error"; $("show_exploremax").innerHTML = "Error"; } else { $("credit_all").innerHTML = credit_all; $("credit_2_all").innerHTML = credit_2_all; $("show_exploremax").innerHTML = show_exploremax; } } </script> I added the HTML. "show_exploremax" is supposed to read the second value from the "investortype" dropdown menu, but I’m having trouble getting it to do that. <form action="" id="explore_cost"> <p> <table width="50%" cellspacing="0px" align="center"> <tbody> <tr> <td colspan="2" align="center"><b>Land Explore Cost</b></td> </tr> <tr> <td>Investor type:</td> <td align="center"><select id="investortype" class="dropdown"> <option value="1:1">Standard</option> <option value="1.2:0.5">Invasive</option> <option value="1.1:0.9">Economist</option> <option value="0.1:9.99">Self-sufficient</option> <option value="1:1">2nd credit</option> <option value="1.1:1">Researcher</option> <!-- I tried to split the values with ":" --> </select> </td> </tr> <tr> <td >Amount of Land:</td> <td align="center"><input id="total" type="text" /></td> </tr> <tr> <td>Land to explore:</td> <td align="center"><input id="toexplore" type="text" /></td> </tr> <tr> <td>Credit Needed:</td> <td> <font id="credit_all"></font></td> </tr> <tr> <td>2nd credit Needed:</td> <td> <font id="credit_2_all"></font></td> </tr> <tr> <td>Explore max:</td> <td> <font id="show_exploremax"></font></td> </tr> <tr><br /> <td align="center" colspan="2"><input type="button" value="Submit" class="button1" onclick="explore()" /></td> </tr> </tbody> EDIT: added html.
document.addEventListener( "DOMContentLoaded", init, false ); function init() { var select = document.getElementById('investortype'); select.addEventListener( "change", function() { var option = select.options[select.selectedIndex]; document.getElementById('show_exploremax').innerText = option.dataset.max; }, false); select.selectedIndex = -1; }; <h4>Pick One</h4> <select id="investortype" class="dropdown"> <option data-credit="1" data-max="1">Standard</option> <option data-credit="1.2" data-max="0.5" >Invasive</option> <option data-credit="1.1" data-max="0.9" >Economist</option> <option data-credit="0.1" data-max="9.99" value="0.1:9.99">Self-sufficient</option> <option data-credit="1" data-max="1">2nd credit</option> <option data-credit="1.1" data-max="1">Researcher</option> </select> <h4>2nd option:</h4> Max: <span id="show_exploremax"></span>
My validation keeps giving me an alert even though I have filled the fields
I have a form like this: <form id="formDaftar" onsubmit="return daftarClick()" method="post"> <table id="tableFormDaftar" border="0" width="400px" height="300px"> <tr> <td>Nama</td> <td><input type="text" name="nama" id="nama" size="39"/></td> </tr> <tr> <td>Alamat</td> <td><textarea cols="40" rows="4" name="alamat" id="alamat"> </textarea></td> </tr> <tr> <td>Tempat/Tanggal Lahir</td> <td><input name="tempatLahir" id="tempatLahir" size="39" type="text"/></td> </tr> <tr> <td>Jenis Kelamin</td> <td><input type="radio" id="radio1" name="gender" id="gender1"/>Laki-laki <input type="radio" id="radio1" name="gender" id="gender2"/>Perempuan </td> </tr> <tr> <td>Pekerjaan</td> <td><input name="pekerjaan" id="pekerjaan" size="39"/></td> </tr> <tr> <td>Status</td> <td> <select id="status" name="status"> <option selected="selected">Lajang</option> <option>Menikah</option> <option>Duda</option> <option>Janda</option> </select></td> </tr> <tr> <td>Agama</td> <td><select id="agama" name="agama"> <option>Islam</option> <option selected="selected">Kristen</option> <option>Katholik</option> <option>Budha</option> <option>Hindu</option> </select></td> </tr> <tr> <td>Kewarganegaraan</td> <td><select id="kwn"> <option selected="selected">Indonesia</option> <option>Asing</option> </select></td> </tr> <tr> <td colspan="2" id="colbutton"> <button name="masukkan" type="submit" id="masukkan">Masukkan</button> <button type="reset" name="batalkan" id="batalkan">Batalkan</button></td> </tr> </table></form> I have a script like this: var nama = document.getElementById("nama").value; var alamat = document.getElementById("alamat").value; var tempatLahir = document.getElementById("tempatLahir").value; var gender; if (document.getElementById("gender1").checked){ gender = document.getElementById("gender1").value; } else if (document.getElementById("gender2").checked){ gender = document.getElementById("gender2").value; } var a = document.getElementById("agama"); var agama = a.options[a.selectedIndex].text; var pekerjaan = document.getElementById("pekerjaan").value; var b = document.getElementById("status"); var status = b.options[b.selectedIndex].text; var c = document.getElementById("kwn"); var kwn = c.options[c.selectedIndex].text; function daftarClick(){ if (nama == ""){ alert("Nama harus diisi!"); return false; } else if (alamat == ""){ alert("Alamat harus diisi!"); return false; } else if (tempatLahir == ""){ alert("Tempat/Tanggal Lahir harus diisi!"); return false; } else if(!document.getElementById("gender1").checked && !document.getElementById("gender2").checked){ alert("Jenis Kelamin harus dipilih!"); return false; } } The problem is that even though I have filled the fields, it keeps showing the alert() saying that I need to fill the fields. I have set the validation which will show an alert if the user had not put any letters in the fields but no matter what I put in the fields, it keeps saying that I haven't put it there. What did I get wrong here?
It is because your variables were declared at page load so then your inputs were empty and the variables got empty values Change it to <script type="text/javascript"> function daftarClick(){ var nama = document.getElementById("nama").value; var alamat = document.getElementById("alamat").value; var tempatLahir = document.getElementById("tempatLahir").value; var gender; if (document.getElementById("gender1").checked){ gender = document.getElementById("gender1").value; } else if (document.getElementById("gender2").checked){ gender = document.getElementById("gender2").value; } var a = document.getElementById("agama"); var agama = a.options[a.selectedIndex].text; var pekerjaan = document.getElementById("pekerjaan").value; var b = document.getElementById("status"); var status = b.options[b.selectedIndex].text; var c = document.getElementById("kwn"); var kwn = c.options[c.selectedIndex].text; if (nama == ""){ alert("Nama harus diisi!"); return false; } else if (alamat == ""){ alert("Alamat harus diisi!"); return false; } else if (tempatLahir == ""){ alert("Tempat/Tanggal Lahir harus diisi!"); return false; } else if(!document.getElementById("gender1").checked && !document.getElementById("gender2").checked){ alert("Jenis Kelamin harus dipilih!"); return false; } } </script>
Try this: <script type="text/javascript"> function daftarClick(){ var nama = document.getElementById("nama").value; var alamat = document.getElementById("alamat").value; var tempatLahir = document.getElementById("tempatLahir").value; var gender; if (document.getElementById("gender1").checked){ gender = document.getElementById("gender1").value; } else if (document.getElementById("gender2").checked){ gender = document.getElementById("gender2").value; } var a = document.getElementById("agama"); var agama = a.options[a.selectedIndex].text; var pekerjaan = document.getElementById("pekerjaan").value; var b = document.getElementById("status"); var status = b.options[b.selectedIndex].text; var c = document.getElementById("kwn"); var kwn = c.options[c.selectedIndex].text; if (nama == ""){ alert("Nama harus diisi!"); return false; } else if (alamat == ""){ alert("Alamat harus diisi!"); return false; } else if (tempatLahir == ""){ alert("Tempat/Tanggal Lahir harus diisi!"); return false; } else if(!document.getElementById("gender1").checked && !document.getElementById("gender2").checked){ alert("Jenis Kelamin harus dipilih!"); return false; } } </script> EDIT: The checkboxes in the HTML have multiple id attributes, which is an error, and thus document.getElementById cannot find them. <form id="formDaftar" onsubmit="return daftarClick()" method="post"> <table id="tableFormDaftar" border="0" width="400px" height="300px"> <tr> <td>Nama</td> <td><input type="text" name="nama" id="nama" size="39"/></td> </tr> <tr> <td>Alamat</td> <td><textarea cols="40" rows="4" name="alamat" id="alamat"> </textarea></td> </tr> <tr> <td>Tempat/Tanggal Lahir</td> <td><input name="tempatLahir" id="tempatLahir" size="39" type="text"/></td> </tr> <tr> <td>Jenis Kelamin</td> <!-- Fix is here, id="radio1" removed from both --> <td><input type="radio" name="gender" id="gender1"/>Laki-laki <input type="radio" name="gender" id="gender2"/>Perempuan </td> </tr> <tr> <td>Pekerjaan</td> <td><input name="pekerjaan" id="pekerjaan" size="39"/></td> </tr> <tr> <td>Status</td> <td> <select id="status" name="status"> <option selected="selected">Lajang</option> <option>Menikah</option> <option>Duda</option> <option>Janda</option> </select></td> </tr> <tr> <td>Agama</td> <td><select id="agama" name="agama"> <option>Islam</option> <option selected="selected">Kristen</option> <option>Katholik</option> <option>Budha</option> <option>Hindu</option> </select></td> </tr> <tr> <td>Kewarganegaraan</td> <td><select id="kwn"> <option selected="selected">Indonesia</option> <option>Asing</option> </select></td> </tr> <tr> <td colspan="2" id="colbutton"> <button name="masukkan" type="submit" id="masukkan">Masukkan</button> <button type="reset" name="batalkan" id="batalkan">Batalkan</button></td> </tr> </table></form>
check this I think it is working as you are expecting https://jsbin.com/ragoces/edit?html,js,output var nama = document.getElementById("nama"); var alamat = document.getElementById("alamat"); var tempatLahir = document.getElementById("tempatLahir"); var gender; if (document.getElementById("gender1").checked){ gender = document.getElementById("gender1").value; } else if (document.getElementById("gender2").checked){ gender = document.getElementById("gender2").value; } var a = document.getElementById("agama"); var agama = a.options[a.selectedIndex].text; var pekerjaan = document.getElementById("pekerjaan").value; var b = document.getElementById("status"); var status = b.options[b.selectedIndex].text; var c = document.getElementById("kwn"); var kwn = c.options[c.selectedIndex].text; function daftarClick(){ if (nama.value === ""){ alert("Nama harus diisi!"); return false; } else if (alamat.value === ""){ alert("Alamat harus diisi!"); return false; } else if (tempatLahir.value === ""){ alert("Tempat/Tanggal Lahir harus diisi!"); return false; } else if(!document.getElementById("gender1").checked && !document.getElementById("gender2").checked){ alert("Jenis Kelamin harus dipilih!"); return false; } }
Add textbox value to multiple select box via JavaScript
I have a textbox in which the user is allowed to enter alphanumeric input. I also have a plus image which on clicking will have to add the entered text to a multiple select box called "add key num" (shown below). After adding a few inputs to the multiple select box "add key num", the user can click the left arrow to add the final list to the "list key num" select box. I am stuck on the JavaScript for adding the text input from the textbox to the first select box. Please advise. Here is the code that I have: <cfparam name="SESSION.stkeynum" default=""> <table border="1" align="center" STYLE="margin-left:120px;" bgcolor="##B0C4DE"> <tr> <td align="center" >ENTER KEY NUM</td> <td align="center" ></td> <td align="center" >ADD KEY NUM</td> <td align="center" ></td> <td align="center" >LIST KEY NUM</td> <td align="center" >AMOUNT</td> </tr> <tr> <td align="center"><input type="text" maxlength ="8" name="vstkeynum" id="vstkeynum" value="" size="15"></td> <td><img src="../images/plus.jpg" alt="Add Key number" onclick="add_cc(document.myform.vstkeynum.value);"></img></td> <td nowrap> <div id="Div1"></div> <select name="keynumber" size="5" multiple style="width: 175px; display: inline;"> <option value="null"> </option> </select> </td> <td><img src="../images/arrow_right.gif" alt="Add To List" onclick="copySelectedOptions_edit(document.myform.keynumber,document.myform.keynumber_selection);"> <hr> <img onclick="removeSelectedOptions_edit(document.myform.keynumber_selection);" src="../images/arrow_left.gif" alt="Delete From List"> </td> <td nowrap> <select name="keynumber_selection" size="5" multiple style="width: 175px;"> </select> </td> <td align="center"><input type="text" maxlength ="8" name="amount" id="amount" value="" size="7"></td> </tr> </table> JavaScript: <script type="text/javascript"> function add_cc(a) { // inital load ajax({ url: 'add_keynum.cfm?vval=' + a, fillDiv: 'Div1', showBusy: true }); document.myform.vstkeynum.value = ""; } </script> <script> function copySelectedOptions_edit(from, to) { var options = new Object(); if (hasOptions(to)) { for (var i = 0; i < to.options.length; i++) { options[to.options[i].value] = to.options[i].text; } } if (!hasOptions(from)) { return; } for (var i = 0; i < from.options.length; i++) { var o = from.options[i]; if (o.selected) { if (options[o.value] == null || options[o.value] == "undefined" || options[o.value] != o.text) { if (!hasOptions(to)) { var index = 0; } else { var index = to.options.length; } to.options[index] = new Option(o.text, o.value, false, false); } } } if ((arguments.length < 3) || (arguments[2] == true)) { sortSelect(to); } from.selectedIndex = -1; to.selectedIndex = -1; } function removeSelectedOptions_edit(from) { if (!hasOptions(from)) { return; } if (from.type == "select-one") { from.options[from.selectedIndex] = null; } else { for (var i = (from.options.length - 1); i >= 0; i--) { var o = from.options[i]; if (o.selected) { from.options[i] = null; } } } from.selectedIndex = -1; } </script> This is the code in add_keynum.cfm: <cfif isDefined("vval") > <cfif trim(vval) neq ""> <cfset SESSION.stkeynum = Listappend(SESSION.stkeynum,vval)> <cfoutput>#SESSION.stkeynum#</cfoutput> <cfelse> <cfif isDefined("session.stkeynum")> <cfoutput>#SESSION.stkeynum#</cfoutput> </cfif> </cfif> </cfif>