JavaScript - Filtering An Array
Hi,
I have an array for all the trousers on my site. It includes the image, name, waist, description, price and buy link. The javascript I use below outputs all products in the array. What I'd like to do is only output products which contain either "Wrangler" or "Levi" in the name string AND "forty" or "thirty" in the waist string. Would anyone know how to change the for loop in order to do this? Code: for(i=0;i<Trousers.length;i++){ $('body').append("<div><img src='"+Trousers[i].ProductImage+"'>"+"<br /><strong>"+Trousers[i].name+"</strong><br /><span style='color: #999; font-size:10px;'>"+Trousers[i].waist+"</span><br />"+Trousers[i].description+"<br /><strong>"+Trousers[i].price+"</strong><br /><a href='"+Trousers[i].buyLink+"'>Add to Basket</a></div>"); } Thanks in advance Similar TutorialsHi , I need a javascript help for the following snippet of html code. The html displays Data in tabular format. There is drop-down at the 'Status" column of this table. If the status is chosen as 'all', then all the rows will be displayed having status ('CO','OK','IN' and '-'). But if the status is selected as 'valid', then the rows having '-' as status will be discarded and only rows having 'CO','OK,'IN' as status will be displayed. The following code is working except for a single row once I select 'valid' as Status from the drop-down. Please notice the cell under "Task" column having value 'checkRsf1' and its immediate right cell containing 'exec' under "Command" column is not coming at all after filtering by "valid". Instead the 'esdnet' and 'IN' (under Method and Status column) are getting shifted to left under wrong column head. Thanks in advance. Below is the html embedded with Javascript. <html> <head> <link rel="stylesheet" type="text/css" href="/css/tree.css" /> </head> <body style="background-color:#fff"> <table border="1" id="mytable"><tr style="background-color:#d0d0d0"> <th align="left">Task</th><th align="left">Command</th><th align="left">Method</th> <th align="left">Status<br><select onchange="showRows(this.value)"> <option value="all">show status</option> <option value="all">all</option> <option value="active">valid</option> </select> </th> <th align="left">Task version</th><th align="left">Tool</th><th align="left">Last changed</th><th align="left">Comments</th></tr> <tr><td rowspan="1">char32</td><td rowspan="1">exec</td><td nowrap="nowrap"> </td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="1">H1p</td><td rowspan="1">exec</td><td nowrap="nowrap"> </td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="1">H1c</td><td rowspan="1">exec</td><td nowrap="nowrap"> </td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="1">adex</td><td rowspan="1">exec</td><td nowrap="nowrap"> </td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="1">ds</td><td rowspan="1">exec</td><td nowrap="nowrap"> </td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="1">mec</td><td rowspan="1">exec</td><td nowrap="nowrap"> </td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="1">admsSim_post</td><td rowspan="1">exec</td><td nowrap="nowrap"> </td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="1">Sim_pre</td><td rowspan="1">exec</td><td nowrap="nowrap"> </td><td nowrap="nowrap"><font color="red">CO</font></td><td nowrap="nowrap">CO</td><td nowrap="nowrap">CO</td><td nowrap="nowrap">Apr 24 </td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="1">amsdSim_post</td><td rowspan="1">exec</td><td nowrap="nowrap"> </td><td nowrap="nowrap"><font color="red">OK</font></td><td nowrap="nowrap">CO</td><td nowrap="nowrap">CO</td><td nowrap="nowrap">Apr 21 </td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="1">amsdSim_pre</td><td rowspan="1">exec</td><td nowrap="nowrap"> </td><td nowrap="nowrap"><font color="red">OK</font></td><td nowrap="nowrap">CO</td><td nowrap="nowrap">CO</td><td nowrap="nowrap">May 3 </td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="1">antdrc</td><td rowspan="1">exec</td><td nowrap="nowrap"> </td><td nowrap="nowrap"><font color="red">CO</font></td><td nowrap="nowrap">CO</td><td nowrap="nowrap">CO</td><td nowrap="nowrap">Apr 24</td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="1">archive</td><td rowspan="1">exec</td><td nowrap="nowrap"> </td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="1">avenue</td><td rowspan="1">exec</td><td nowrap="nowrap"> </td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="1">cdl</td><td rowspan="1">exec</td><td nowrap="nowrap"> </td><td nowrap="nowrap"><font color="red">IN</font></td><td nowrap="nowrap">CO</td><td nowrap="nowrap">CO</td><td nowrap="nowrap">Apr 2</td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="1">cdl_pre</td><td rowspan="1">exec</td><td nowrap="nowrap"> </td><td nowrap="nowrap"><font color="red">CO</font></td><td nowrap="nowrap">CO</td><td nowrap="nowrap">CO</td><td nowrap="nowrap">Apr 8</td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="5">checkRsf</td><td rowspan="5">exec</td><td nowrap="nowrap">antdrc</td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td nowrap="nowrap">drc</td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td nowrap="nowrap">erc</td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td nowrap="nowrap">esdnet</td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td nowrap="nowrap">lvs</td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="5">checkRsf1</td><td rowspan="5">exec</td><td nowrap="nowrap">antdrcres</td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td nowrap="nowrap">drc</td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td nowrap="nowrap">erc</td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td nowrap="nowrap">esdnet</td><td nowrap="nowrap">IN</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td nowrap="nowrap">lvs</td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="5">checkRsf2</td><td rowspan="5">exec</td><td nowrap="nowrap">antdrcres</td><td nowrap="nowrap">IN</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td nowrap="nowrap">drc</td><td nowrap="nowrap">IN</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td nowrap="nowrap">erc</td><td nowrap="nowrap">IN</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td nowrap="nowrap">esdnet</td><td nowrap="nowrap">IN</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td nowrap="nowrap">lvs</td><td nowrap="nowrap">IN</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="2"> designSync</td><td rowspan="2">exec</td><td nowrap="nowrap">dssc_layout</td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td nowrap="nowrap">dssc_schematic</td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="1">diosearch</td><td rowspan="1">exec</td><td nowrap="nowrap"> </td><td nowrap="nowrap">-</td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td><td nowrap="nowrap"> </td></tr> <tr><td rowspan="1">drc</td><td rowspan="1">exec</td><td nowrap="nowrap"> </td><td nowrap="nowrap"><font color="red">IN</font></td><td nowrap="nowrap">CO</td><td nowrap="nowrap">CO</td><td nowrap="nowrap">Apr 3</td><td nowrap="nowrap"> </td></tr> </table> <script type="text/javascript"> mytab=document.getElementById("mytable") function showRows(val){ for (var i = 0; i < mytab.rows.length; i++) { if (val=="all"){ mytab.rows[i].style.display="table-row" } else { if (mytab.rows[i].innerHTML.match("-")){ mytab.rows[i].style.display="none" } } } } </script> </body> </html> Hi, I have a large e-commerce site that runs on a database. There are many products on the site which have 'variables' attached to them, e.g. colour, width, height etc. I have a script in place to sort the products depending on these variables. Apologies for the length of the code. Code: (function(i) {var u =navigator.userAgent;var e=/*@cc_on!@*/false; var st = setTimeout;if(/webkit/i.test(u)){st(function(){var dr=document.readyState; if(dr=="loaded"||dr=="complete"){i()}else{st(arguments.callee,10);}},10);} else if((/mozilla/i.test(u)&&!/(compati)/.test(u)) || (/opera/i.test(u))){ document.addEventListener("DOMContentLoaded",i,false); } else if(e){ ( function(){var t=document.createElement('doc:rdy');try{t.doScroll('left'); i();t=null;}catch(e){st(arguments.callee,0);}})();}})(sortinit); var prodlines = new Array(); // for Products var spanids = new Array(); var lastsorttype = 'default'; var bReadyToSort = false; var searchlines = new Array(); // for Search Results var searchspanids = new Array(); var searchlastsorttype = 'default'; var bSearchReadyToSort = false; var sectlines = new Array(); // for Sections var sectspanids = new Array(); var sectlastsorttype = 'default'; var bSectReadyToSort = false; var bSortInitalised = false; // Search Results Related function searchreseq(seqtype){ // resequence search results if ( ! (bSearchReadyToSort || bPageIsLoaded) ) { alert('Please wait until the page has fully loaded. (SRC)'); return false; } searchlines.length = 0; searchspanids.length = 0; var spans = document.getElementsByTagName('span'); for ( var i=0; i<spans.length; i++ ) { if ( spans[i].id.indexOf('searchsortline_') == 0 ) { searchspanids.push(spans[i]); searchlines.push({ id: spans[i].id, price: spans[i].getAttribute('price').replace(/[^\d\.]/g,''), seq: spans[i].getAttribute('seq'), pname: spans[i].getAttribute('pname'), phtml: spans[i].innerHTML }); } } if ( seqtype == 'lowhigh' ) searchlines.sort(function(a,b){return a.price - b.price}); if ( seqtype == 'highlow' ) searchlines.sort(function(a,b){return b.price - a.price}); if ( seqtype == 'default' ) searchlines.sort(function(a,b){return a.seq - b.seq}); if ( seqtype == 'alpha' ) searchlines.sort(function(a,b){return (a.pname <= b.pname) ? -1 : 1;}); if ( seqtype == 'reverse' ) searchlines.sort(function(a,b){return (b.pname <= a.pname) ? -1 : 1;}); searchlastsorttype = seqtype; setCookie('SEARCHSORTTYPE', searchlastsorttype); searchshowsorttype(); for ( var i=0; i<searchlines.length; i++ ) { searchspanids[i].innerHTML = searchlines[i].phtml; searchspanids[i].id = searchlines[i].id; searchspanids[i].setAttribute('price', searchlines[i].price); searchspanids[i].setAttribute('seq', searchlines[i].seq); searchspanids[i].setAttribute('pname', searchlines[i].pname); } return false; } function searchshowsorttype(){ // highlight the type of sort that's currently in use if ( document.getElementById('searchsortdropdown') ) // see if we're using drop-downs { var dropdowns = document.getElementsByTagName('select'); // all drop-downs for ( var i=0; i<dropdowns.length; i++ ) { if ( dropdowns[i].name == 'searchsortdropdown' ) // relevant one { var sdd = dropdowns[i]; for ( var j=0; j<sdd.options.length; j++ ) { if ( sdd.options[j].value == searchlastsorttype ) sdd.selectedIndex = j; } } } } else { var buttonlinks = document.getElementsByTagName('input'); // looking for buttons for ( var i=0; i<buttonlinks.length; i++ ) { if ( buttonlinks[i].name == 'searchsortlink' ) { buttonlinks[i].className = ( buttonlinks[i].id == searchlastsorttype ) ? 'searchsortactive' : 'searchsortinactive'; } } var buttonlinks = document.getElementsByTagName('a'); // looking for A tags for ( var i=0; i<buttonlinks.length; i++ ) { if ( buttonlinks[i].name == 'searchsortlink' ) { buttonlinks[i].className = ( buttonlinks[i].id == searchlastsorttype ) ? 'searchsortactive' : 'searchsortinactive'; } } } } // Section Related function sectionseq(seqtype){ // resequence Sections if ( ! (bSectReadyToSort || bPageIsLoaded) ) { alert('Please wait until the page has fully loaded. (SECT)'); return false; } sectlines.length = 0; sectspanids.length = 0; var spans = document.getElementsByTagName('span'); for ( var i=0; i<spans.length; i++ ) { if ( spans[i].id.indexOf('sectionline_') == 0 ) { sectspanids.push(spans[i]); sectlines.push({ id: spans[i].id, price: spans[i].getAttribute('price'), seq: spans[i].getAttribute('seq'), pname: spans[i].getAttribute('pname'), phtml: spans[i].innerHTML }); } } if ( seqtype == 'lowhigh' ) sectlines.sort(function(a,b){return a.price - b.price}); if ( seqtype == 'highlow' ) sectlines.sort(function(a,b){return b.price - a.price}); if ( seqtype == 'default' ) sectlines.sort(function(a,b){return a.seq - b.seq}); if ( seqtype == 'alpha' ) sectlines.sort(function(a,b){return (a.pname <= b.pname) ? -1 : 1;}); if ( seqtype == 'reverse' ) sectlines.sort(function(a,b){return (b.pname <= a.pname) ? -1 : 1;}); sectlastsorttype = seqtype; setCookie('SECTSORTTYPE', sectlastsorttype); sectshowsorttype(); for ( var i=0; i<sectlines.length; i++ ) { sectspanids[i].innerHTML = sectlines[i].phtml; sectspanids[i].id = sectlines[i].id; sectspanids[i].setAttribute('price', sectlines[i].price); sectspanids[i].setAttribute('seq', sectlines[i].seq); sectspanids[i].setAttribute('pname', sectlines[i].pname); } return false; } function sectshowsorttype(){ // highlight the type of sort that's currently in use if ( document.getElementById('sectionsortdropdown') ) // see if we're using drop-downs { var dropdowns = document.getElementsByTagName('select'); // all drop-downs for ( var i=0; i<dropdowns.length; i++ ) { if ( dropdowns[i].name == 'sectionsortdropdown' ) // relevant one { var sdd = dropdowns[i]; for ( var j=0; j<sdd.options.length; j++ ) { if ( sdd.options[j].value == sectlastsorttype ) sdd.selectedIndex = j; } } } } else { var buttonlinks = document.getElementsByTagName('input'); // looking for buttons for ( var i=0; i<buttonlinks.length; i++ ) { if ( buttonlinks[i].name == 'sectsortlink' ) { buttonlinks[i].className = ( buttonlinks[i].id == sectlastsorttype ) ? 'sectsortactive' : 'sectsortinactive'; } } var buttonlinks = document.getElementsByTagName('a'); // looking for A tags for ( var i=0; i<buttonlinks.length; i++ ) { if ( buttonlinks[i].name == 'sectsortlink' ) { buttonlinks[i].className = ( buttonlinks[i].id == sectlastsorttype ) ? 'sectsortactive' : 'sectsortinactive'; } } } } // Product Related function reseq(seqtype, gotoanchor){ // resequence products if ( ! (bReadyToSort || bPageIsLoaded) ) { alert('Please wait until the page has fully loaded. (PROD)'); return false; } prodlines.length = 0; spanids.length = 0; var spans = document.getElementsByTagName('span'); for ( var i=0; i<spans.length; i++ ) { if ( spans[i].id.indexOf('sortline_') == 0 ) { spanids.push(spans[i]); prodlines.push({ id: spans[i].id, price: spans[i].getAttribute('price'), seq: spans[i].getAttribute('seq'), pname: spans[i].getAttribute('pname'), brand: spans[i].getAttribute('brand'), capacity: spans[i].getAttribute('capacity'), height: spans[i].getAttribute('height'), width: spans[i].getAttribute('width'), depth: spans[i].getAttribute('depth'), colour: spans[i].getAttribute('colour'), energyrating: spans[i].getAttribute('energyrating'), spinspeed: spans[i].getAttribute('spinspeed'), phtml: spans[i].innerHTML }); } } if ( seqtype == 'lowhigh' ) prodlines.sort(function(a,b){return a.price - b.price}); if ( seqtype == 'highlow' ) prodlines.sort(function(a,b){return b.price - a.price}); if ( seqtype == 'default' ) prodlines.sort(function(a,b){return a.seq - b.seq}); if ( seqtype == 'alpha' ) prodlines.sort(function(a,b){return (a.pname <= b.pname) ? -1 : 1;}); if ( seqtype == 'reverse' ) prodlines.sort(function(a,b){return (b.pname <= a.pname) ? -1 : 1;}); if ( seqtype == 'brand' ) prodlines.sort(function(a,b){return (a.brand <= b.brand) ? -1 : 1;}); if ( seqtype == 'capacity' ) prodlines.sort(function(a,b){return (b.capacity <= a.capacity) ? -1 : 1;}); if ( seqtype == 'height' ) prodlines.sort(function(a,b){return (a.height <= b.height) ? -1 : 1;}); if ( seqtype == 'width' ) prodlines.sort(function(a,b){return (a.width <= b.width) ? -1 : 1;}); if ( seqtype == 'depth' ) prodlines.sort(function(a,b){return (a.depth <= b.depth) ? -1 : 1;}); if ( seqtype == 'colour' ) prodlines.sort(function(a,b){return (a.colour <= b.colour) ? -1 : 1;}); if ( seqtype == 'energyrating' ) prodlines.sort(function(a,b){return (a.energyrating <= b.energyrating) ? -1 : 1;}); if ( seqtype == 'spinspeed' ) prodlines.sort(function(a,b){return (b.spinspeed <= a.spinspeed) ? -1 : 1;}); lastsorttype = seqtype; setCookie('SORTTYPE', lastsorttype); showsorttype(); for ( var i=0; i<prodlines.length; i++ ) { spanids[i].innerHTML = prodlines[i].phtml; spanids[i].id = prodlines[i].id; spanids[i].setAttribute('price', prodlines[i].price); spanids[i].setAttribute('seq', prodlines[i].seq); spanids[i].setAttribute('pname', prodlines[i].pname); spanids[i].setAttribute('brand', prodlines[i].brand); spanids[i].setAttribute('capacity', prodlines[i].capacity); spanids[i].setAttribute('height', prodlines[i].height); spanids[i].setAttribute('width', prodlines[i].width); spanids[i].setAttribute('depth', prodlines[i].depth); spanids[i].setAttribute('colour', prodlines[i].colour); spanids[i].setAttribute('energyrating', prodlines[i].energyrating); spanids[i].setAttribute('spinspeed', prodlines[i].spinspeed); } if ( gotoanchor && location.hash ) scrolltoanchor(location.hash); // Product Page just loaded and Anchor so we need to reposition // // Some Drillpine add-ons need reactivated after the DOM has been fiddled with // // Tabber (needs V210 onwards) - reset Tabber if ( window.resetallpanes ) resetallpanes(); // Tabber V3 if ( window.jqinittabber ) jqinittabber(); // Lightbox / Slimbox if ( window.Slimbox && window.Slimbox.scanPage ) Slimbox.scanPage(); // Lightbox / Slimbox2 if ( (typeof(jQuery) == 'function') && (typeof(jQuery.slimbox) == 'function') ) { $("a[rel^='lightbox']").slimbox({/* Put custom options here */}, null, function(el) { return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel)); }); } return false; } function showsorttype(){ // highlight the type of sort that's currently in use if ( document.getElementById('sortdropdown') ) // see if we're using drop-downs { var dropdowns = document.getElementsByTagName('select'); // all drop-downs for ( var i=0; i<dropdowns.length; i++ ) { if ( dropdowns[i].name == 'sortdropdown' ) // relevant one { var sdd = dropdowns[i]; for ( var j=0; j<sdd.options.length; j++ ) { if ( sdd.options[j].value == lastsorttype ) sdd.selectedIndex = j; } } } } else { var buttonlinks = document.getElementsByTagName('input'); // looking for buttons for ( var i=0; i<buttonlinks.length; i++ ) { if ( buttonlinks[i].name == 'sortlink' ) { buttonlinks[i].className = ( buttonlinks[i].id == lastsorttype ) ? 'sortactive' : 'sortinactive'; } } var buttonlinks = document.getElementsByTagName('a'); // looking for A tags for ( var i=0; i<buttonlinks.length; i++ ) { if ( buttonlinks[i].name == 'sortlink' ) { buttonlinks[i].className = ( buttonlinks[i].id == lastsorttype ) ? 'sortactive' : 'sortinactive'; } } } } // General code function hidetagswithid(tag, id){ // hide all tags with ID var alltags = document.getElementsByTagName(tag); // all such tags for ( var i=0; i<alltags.length; i++ ) { if ( alltags[i].id == id ) // with ID { alltags[i].style.display = 'none'; // hide entire control alltags[i].style.visibility = 'hidden'; // hide entire control } } } function scrolltoanchor(anc){ // reposition to display anchor var ancs = document.getElementsByTagName('a'); anc = anc.replace(/.*\#/,''); // Search highlight passes duplicate anchors - lose one if ( ancs[anc] ) ancs[anc].scrollIntoView(true); } function sortinit(){ // called when DOM is loaded if (arguments.callee.done) return; arguments.callee.done = true; bSortInitialised = true; // Products if ( document.getElementById('sortwidget') ) { bReadyToSort = true; lastsorttype = getCookie('SORTTYPE'); if ( lastsorttype == null ) lastsorttype = 'default'; setCookie('SORTTYPE', lastsorttype); showsorttype(); if ( lastsorttype != 'default' ) { reseq(lastsorttype, true); } } // Search Results if ( document.getElementById('searchsortwidget') ) { bSearchReadyToSort = true; searchlastsorttype = getCookie('SEARCHSORTTYPE'); if ( searchlastsorttype == null ) searchlastsorttype = 'default'; setCookie('SEARCHSORTTYPE', searchlastsorttype); searchshowsorttype(); if ( searchlastsorttype != 'default' ) { searchreseq(searchlastsorttype); } } // Sections if ( document.getElementById('sectsortwidget') ) { // check for no prices available and hide buttons if so var spans = document.getElementsByTagName('span'); var rtot = 0; for ( var i=0; i<spans.length; i++ ) { if ( spans[i].id.indexOf('sectionline_') == 0 ) { var price = spans[i].getAttribute('price'); rtot += (price != null) ? price - 0 : 0; } } if ( rtot == 0 ) { if ( SectionSortLinksFinalOnly ) // do we only show links on final Sections { hidetagswithid('span', 'sectsortwidget'); // hide all sort widgets } else { if ( document.getElementById('sectpricelinks') ) // button or text links { hidetagswithid('span', 'sectpricelinks'); } if ( document.getElementById('sectionsortdropdown') ) // drop-down list { var dropdowns = document.getElementsByTagName('select'); // all drop-downs for ( var i=0; i<dropdowns.length; i++ ) { if ( dropdowns[i].id == 'sectionsortdropdown' ) // relevant one { var opts = dropdowns[i].options; for ( var j=opts.length; j>0; j-- ) // scan backwards as we're deleting items { if ( (opts[j - 1].value == "lowhigh") || (opts[j - 1].value == "highlow") ) { opts[j - 1] = null; } } } } } } } bSectReadyToSort = true; sectlastsorttype = getCookie('SECTSORTTYPE'); if ( sectlastsorttype == null ) sectlastsorttype = 'default'; setCookie('SECTSORTTYPE', sectlastsorttype); sectshowsorttype(); // only resequence if button showing and we've a non default sequence if ( (document.getElementById('sectsortwidget').style.display != 'none') && (sectlastsorttype != 'default') ) { sectionseq(sectlastsorttype); } } } function onloadsortinit(){ // optionally called on onload if ( ! bSortInitalised ) sortinit(); // only init if it's not already run } // in case we cannot activate on DOM loaded if (window.attachEvent) // IE { window.attachEvent("onload", onloadsortinit); } else // DOM { window.addEventListener("load", onloadsortinit, false); } I didn't write this script and I'm not a JavaScript expert, so i don't know how to edit it properly. I want to change it's function from sorting to filtering, i.e. something along the lines of a slice function. Any advice would be much appreciated. I am working on a page where the user will select a location from a dynamically generated dropdown list. I was able to create the php multidimensional array (tested and working) from a MySql database using the users information at login, but I'm having problems converting it to a javascript multidimensional array. I need to be able to access variables that I can pass to a number of text fields within an html form. For instance, if a user belongs to a company with multiple addresses, I need to be able to let them select the address they need to prepopulate specific text fields. php array creation: Code: if ($row_locations) { while ($row_locations = mysql_fetch_assoc($locations)) { $mail[$row_locations['comp_id']]=array('mailto'=>$row_locations['mailto'], 'madd'=>$row_locations['madd'], 'madd2'=>$row_locations['madd2'], 'mcity'=>$row_locations['mcity'], 'mstate'=>$row_locations['mstate'], 'mzip'=>$row_locations['mzip'], 'billto'=>$row_locations['billto'], 'badd'=>$row_locations['badd'], 'badd2'=>$row_locations['badd2'], 'bcity'=>$row_locations['bcity'], 'bstate'=>$row_locations['bstate'], 'bzip'=>$row_locations['bzip']); } } javascript function - this should create the array and send variables to text fields. Code: function updateAddress() { var mail = $.parseJSON(<?php print json_encode(json_encode($mail)); ?>); { if (comp_id in mail) { document.getElementById('mailto').value=mail.comp_id.mailto.value; document.getElementById('madd').value=mail.comp_id.madd.value; document.getElementById('madd2').value=mail.comp_id.madd2.value; document.getElementById('mcity').value=mail.comp_id.mcity.value; document.getElementById('mstate').value=mail.comp_id.mstate.value; document.getElementById('mzip').value=mail.comp_id.mzip.value; } else { document.getElementById('mailto').value=''; document.getElementById('madd').value=''; document.getElementById('madd2').value=''; document.getElementById('mcity').value=''; document.getElementById('mstate').value=''; document.getElementById('mzip').value=''; } } } Where is this breaking? Thanks in advance. I have the following array that contain the people who are on off on certain time: Code: var all = [ ["1234", "Jim", "2011-10-23 00:00:00", "2011-10-25 07:00:00"], ["1235", "Jack", "2011-10-21 00:00:00", "2011-10-21 08:00:00"], ["1236", "Jane", "2011-10-11 00:00:00", "2011-10-11 00:30:00"], ["1237", "June", "2011-10-20 00:00:00", "2011-10-20 12:00:00"], ["1238", "Jill", "2011-10-14 00:00:00", "2011-10-14 11:00:00"], ["1239", "John", "2011-10-16 00:00:00", "2011-10-16 10:30:00"], ["1240", "Jacab", "2011-10-19 00:00:00", "2011-10-20 08:30:00"] ]; The above array, I wish to use javascript to insert into the FullCalendar (http://arshaw.com/fullcalendar/). I notice that the only way seems to be using the FullCalendar array style (or structure) as follows: Code: $('#calendar').fullCalendar({ events: [ { title : 'event1', start : '2010-01-01' }, { title : 'event2', start : '2010-01-05', end : '2010-01-07' }, { title : 'event3', start : '2010-01-09 12:30:00', allDay : false // will make the time show } ] }); So, the question is: How do I insert my array to match with the FullCalendar array? cause FullCalendar array had a different array structure from my array - and I don't think so that I can write in this way: Code: $('#calendar').fullCalendar({ events:all }); Appreciate any help provided. Hey guys, I'm hoping this is possible or that there is an easier way to do this. I'm having an issue with displaying data from one array that contains information about users in a table that is controlled by a different array. Is it possible to do this or is this use of arrays to display the data the wrong approach? The table is located on one webpage, I simply want to extract one piece of information that I have placed in the initial array as part of the login script that contains user information (for validation for login etc) and display it in a table on the new webpage that is opened as a result of successful validation of the user details. I'm completely stumped and after many attempts I just can't seem to get it to work. I can't seem to figure out how to accomplish this. In my website, I would like the user to input text into a single or multiple textbox(es) and then have the contents of the textbox(es) stored to either a variable or an array. Then I would like to have that variable/array compared to other arrays. Basically, the user is searching for items in a database. The user can search for as many or as little items as they want. Then the item(s) will be compared to multiple arrays to find out if what the user wants is in the database. So for example, let's say the user is searching for recipes that have all or part of these ingredients: chicken, broccoli, lemon, honey. So, there would have been a total of 4 textboxes...one for each ingredient. These ingredients are stored to an array..lets call it ingredient(). In the database of recipes, each recipe has its own array which includes the ingredients needed to make the recipe, we'll call them tag1(), tag2(), and tag3(). Now, I want the array, ingredient(), to be compared to each of the "tag" arrays to see if any of the "tag" arrays include exactly match the ingredient() tag in part or in whole. Is this possible? Hi, Here is a working code to copy 2d php array to 2d javascript array. Code: <html> <head> <?php for($i = 0; $i < 3; $i++) { for($j = 0; $j < 2; $j++) {$quest[$i][$j] = $i*10+$j;} } ?> <script type="text/javascript"> var questions = new Array(3); for (var i = 0; i < 3; i++) { questions[i] = new Array(2); } questions[0] = ["<?php echo join("\", \"", $quest[0]); ?>"]; questions[1] = ["<?php echo join("\", \"", $quest[1]); ?>"]; questions[2] = ["<?php echo join("\", \"", $quest[2]); ?>"]; document.write(questions[0][0] + "<br />"); document.write(questions[0][1] + "<br />"); document.write(questions[1][0] + "<br />"); document.write(questions[1][1] + "<br />"); document.write(questions[2][0] + "<br />"); document.write(questions[2][1] + "<br />"); </script> </head> </html> Now,here's the thing.Notice these lines in the code questions[0] = ["<?php echo join("\", \"", $quest[0]); ?>"]; questions[1] = ["<?php echo join("\", \"", $quest[1]); ?>"]; questions[2] = ["<?php echo join("\", \"", $quest[2]); ?>"]; I would like to put these lines in a loop,something like for (var i = 0; i < 3; i++) { questions[i] = ["<?php echo join("\", \"", $quest[i]); ?>"]; } But even after a lot of efforts I am unable to do so,what am I doing wrong?Thanks Hi, In a nutshell,can anyone tell me how to copy a 2d (two dimensional ,2 dimensional) php array to 2d javascript array?I would be obliged if anyone can provide me a method of doing that OR I have written a code to copy a 2d php array to a 2d javascript array.It is working but there is one problem(please see the following).Can anyone tell me what I am doing wrong here? The 2d php array is $quest[100][6] and the 2d javascript array is questions[100][6] . I have written the javascript code inside the <?php....?> itself using echo "<script language="javascript" type="text/javascript">.......</script>; Now ,inside the javascript,when I try to copy the 2d php array to the 2d javascript array using the following method it works questions[0]= ["<?php echo join("\", \"", $quest[0]); ?>"]; questions[1]= ["<?php echo join("\", \"", $quest[1]); ?>"]; ... and so on However, if I try to do the same using the following method it does not work for (var i= 0; i <= 99; i++) { questions[i]= ["<?php echo join("\", \"", $quest[i]); ?>"]; } Why is that?What mistake am I making?Any help will be deeply appreciated.Thanks -----------------------------THE CODE------------------------------------ <?php Access database and store result of mysq_query in $result....... $result = mysql_query($query); for ( $count = 0; $count <= 99; $count++) { $quest[$count]=mysql_fetch_array($result,MYSQL_NUM);; } echo "<script language="javascript" type="text/javascript"> var questions = new Array(100); for (var i = 0; i <100; i++) { questions[i] = new Array(6); } /*The following method of copying 2d php array to 2d javascript array is not working for ( var i = 0; i <= 99; i++) { questions[i]= ["<?php echo join("\", \"", $quest[i]); ?>"]; } */ /*The following method ,however,is working*/ questions[0]= ["<?php echo join("\", \"", $quest[0]); ?>"]; questions[1] = ["<?php echo join("\", \"",$quest[1]); ?>"]; questions[2] = ["<?php echo join("\", \"",$quest[2]); ?>"]; ....and so on </script>"; mysql_close($db_server); ?> Hi, i can't find the mistake in my little script hope someone can help me. PHP Code: <?php /* -------------------- read thumbfolder -------------------- */ function isRdyPfD($filename){ if ($filename == '.' || $filename == '..') { // To-Top-Dir return false; } $ext = explode(".",$filename); $ext = $ext[sizeof($ext) - 1]; $allowedformats = array ( 'jpg', 'png', 'jpeg', 'gif' ); return in_array($ext,$allowedformats); } function getPicsfromDir($dir){ /* array with names of the pictures in $dir */ if (is_dir($dir)) { if ($dh = opendir($dir)) { $filearray = array(); while (($file = readdir($dh)) !== false) { if (isRdyPfD($file) === true) { $filearray[] = $file; } } closedir($dh); return $filearray; } } else { return false; } } // End Function $thumbs = getPicsfromDir("./images/thumbs/"); /* -------------------- thumbfolder -------------------- */ echo "<div id='thumbslider'>\n"; echo "<ul id='thumbs'>\n"; for($i = 0; $i < count($thumbs); $i++){ echo "<li><img src=\"./images/thumbs/$thumbs[$i]\" onclick=\"thumbClick($i)\" /></li>\n"; } echo "</ul>\n"; echo "</div>\n"; /* -------------------- big size images folder -------------------- */ $bigSizeImages = getPicsfromDir("./images/"); //print_r($bigSizeImages); $jsValue = ''; for ($j=0; $j < count($bigSizeImages); $j++){ $jsValue = $jsValue . $bigSizeImages[$j]; if ($j < (count($bigSizeImages)-1)) { $jsValue = $jsValue . ","; } } ?> <script type="text/javascript"> images = new Array(<?php echo $jsValue ?>); function thumbClick(pos){ //alert(pos); alert(images[pos]); } </script> I can't trace the images array values? thanks for a feedback!!! I'm having major pains trying to figure this out. I'm kind of new to Javascript, I need to open a text file from an external server, store each line in an array, then search that array for a certain word (HIGH), and if it exists then write something to the webpage, and if not, write something else. Here is what I have so far: Code: <html> <head> <title>Test</title> <script> <!-- function test(x) { if (wxd1txt.readyState === 4 && wxd1txt.status === 200) { // Makes sure the document is ready to parse and Makes sure it's found the file. var wxd1text = wxd1txt.responseText; var wxd1array = wxd1txt.responseText.split("\n"); // Will separate each line into an array var wxd1high = wxd1array.toString(); //Converting the String content to String //var highsearchreg = new RegExp("HIGH"); //var wxd1high = wxd1array[x].search(highsearchreg); document.write(wxd1high); if (wxd1high.search("HIGH") >= 0){ document.write("HIGH RISK");} else { document.write("NO RISK");} } } //--> </script> </head> <body> Hi! <script> <!-- var Today = new Date(); var ThisDay = Today.getDate(); var ThisMonth = Today.getMonth()+1; var ThisYear = Today.getYear(); var Hour = Today.getHours(); var Day2 = Today.getDate()+1; var Day3 = Today.getDate()+2; if (navigator.appName != "Microsoft Internet Explorer") { ThisYear = ThisYear + 1900;} if (ThisMonth < 10) { ThisMonth = "0" + ThisMonth;} if (ThisDay < 10) { ThisDay = "0" + ThisDay;} if (Hour == 2 || Hour == 22 || Hour == 23 || Hour == 0 || Hour == 1) { var wxHourd1 = 0600} else if (Hour >= 3 && Hour <= 10) { var wxHourd1 = 1300;} else if (Hour >= 11 && Hour <= 13) { var wxHourd1 = 1630;} else if (Hour >= 14 && Hour <= 16) { var wxHourd1 = 2000;} else if (Hour >= 17 && Hour <= 21) { var wxHourd1 = 0100;} //var wxurld1 = "http://www.spc.noaa.gov/products/outlook/archive/"+ThisYear+"/KWNSPTSDY1_"+ThisYear+""+ThisMonth+""+ThisDay+""+wxHourd1+".txt"; var wxurld1 = "http://www.spc.noaa.gov/products/outlook/archive/2010/KWNSPTSDY1_201005101300.txt" //(High risk day for testing) //document.write(wxurld1); //Use this to verify this section is working if (window.XMLHttpRequest) { wxd1txt=new XMLHttpRequest(); } else // IE 5/6 { wxd1txt=new ActiveXObject("Microsoft.XMLHTTP"); } wxd1txt.open("GET", wxurld1, true); wxd1txt.onreadystatechange = test(); // --> </script> </body> </html> When added to a webpage, nothing shows up except the "Hi!" and there are no errors in the Javascript Console in Google Chrome. Is this possible with Javascript, and if so, what am I doing wrong or not doing? Also, I have 2 URLs, one is a text file that has the HIGH text I want for an example, the other is the current file, which shouldn't have HIGH in it (unless the weather in the US turns really bad) Hello I'm have some javascript objects with arrays that should be transferred as php array. Its posted by ajax httpRequest. How can I return php array, from the javascript? JS Code: ['asd1', 'asd2', 'asd3'] via Ajax => PHP PHP Code: array('asd1', 'asd2', 'asd3'); Hi, i have an input value array for customer ids, i also need to make a copy of that array into another input element on submit to use for another process. So far here is what i have come up with regarding the js the current html is this Code: <!-- original input --> <input id="cb<?php echo $i;?>" name="cid[]" value="<?php echo $commissions[$i]['id'];?>" onclick="isChecked(this.checked);" type="checkbox" /> <!-- this is new html input which is outside the $i loop that i need a copy of cid[] stored on submit <input type="hidden" name="ids[]" onsubmit="valueTovalue();" /> <!-- here is the js so far --> <script type="text/javascript"> function valueTovalue() { var valin = document.adminForm.cid.value; //cid is the array cid[] document.adminForm.ids.value = valin; //copy the array to the new input element array ids } </script> so basically if they click 1 cid then ids will also show 1, if they click all cid then ids will show all as well. will that work the way i have it, am i even close here.. thanks update: what i have seems to work fine for text values or string values but not for arrays, what am i missing that it wont copy an array? Array ( [cid] => Array ( [0] => 1 ) [task] => remarksonly [remark] => hhhyyy [boxchecked] => 1 [ids] => Array ( [0] => ) ) am i getting closer lol Code: function valueTovalue() { var valin = cid.concat; document.adminForm.ids.value = valin.concat; return(true); this has to be close i hope Code: document.adminForm.ids.value = [].concat(document.adminForm.cid); OK so I am trying to put together a function that takes an array and then takes the square root of each number in old array and writes to the screen the new array of squared numbers. Not looking for answer, just what I need to do for the logic in the for statement.
Code: <html> <head> <script type="text/javascript"> <!--- var Jane=new Array ("Female","Red","Lipstick","28",","45789546"); var Mary=new Array ("Female","Orange","Lipstick","25",","45789547"); var Elle=new Array ("Female","Blue","Lipstick","21",","45789548"); document.write(Jane[0]); document.write(Jane[1]); document.write(Jane[2]); document.write(Jane[3]); document.write(Jane[4]); //--> </script> <html> Please help me with the above code. I would like to have a prompt so I can enter one of the three girls name (array) then it would display all the elements. For example: Enter a name: Jane the output would be : Female Red Lipstick 28 45789546 Please help. Thank you very much. Sorry if this question sounds dumb but I'm a JS noob. Basically I need to create a script that will take three numerical grades entered from a prompt, as well as the student's fictitious name; then take the average of the highest and lowest grades. So far I've gotten it to: Accept then display the student's name Take three grades then figure out and display the average of all three. I still need it to: Calculate and Display the average of the highest and lowest scores Display all three scores that were entered I assume that I'll have to instantiate an array, then input values from the prompt, but everything I have tried has failed. See my script below. <script type = "text/javascript"> <!-- var gradeCounter, gradeValue, total, average, linebreak = "<br />", name, grade; total = 0; gradeCounter = 0; name = window.prompt("Input Student Name") grade = window.prompt( "Input Numerical Grade, -1 to Quit:", "0" ); gradeValue = parseInt( grade ); while ( gradeValue != -1 ) { total = total + gradeValue; gradeCounter = gradeCounter + 1; grade = window.prompt("Input Grade, -1 to Quit:", "0"); gradeValue = parseInt( grade ); } if ( gradeCounter != 0 ) { average = total / gradeCounter; document.write(name) document.write(linebreak) document.write("Average: " + average); } else document.write( "<p>No grades were entered</p>" ); // --> </script> Hi, I'm trying to create a sort of database, consisting of an empty array, within which/for each i, there is an associative array of 3 properties. In other words, a empty muti-dimensional associative array... I'm having issues inputting into this second layer - I'm possibly getting confused with arrays, objects, functions... Code: var dbase = new Array() for(i in dbase) { dbase[i] = [ artist, name, url ] dbase[i].artist = ("Please enter the artist's name") } Im trying to create a 2d array with these two rows - Example1, Example2, Example3 Example4, Example5, Example6 To print I use this for loop- Code: for(i = 0; i < 2; i++) { for( j = 0 ; j < 3; j++) { document.write(array[i][j]); } } I dont Understand how to create the actual 2d array though. Can someone please shed some light on the situation. Thanks alot I'm trying to find Array's with the Cities of U.S. States (respectively) built. I.E. Alabama - Abbeville Abel Abernant Acmar Adamsville etc....for all 50 states. Does such an Array already exist? this might be PHP as well, so I apologize if this isn't the proper location to post just such things. So if I create an array like below how can I test whether the value in "field1" is in the array? var field1 = document.theForm.car.value; var myCars = new Array(); var myCars=["Saab","Volvo","BMW"]; |