Here is my code, simply I am retrieving an array from the back end, and displaying in a select element, and just checking if an attribute is true then I show an input of expiry date, else I hide it
this is working code...all I need to do is just simplify, just need to create 2 helper/ reusable functions so I can call them ... because as you can see, there is repetitive code...
I tried splitting them to two functions, one to search if this item actually exists and if so it returns the item, if not it returns false...and the other will check if that item actually expires and if it does, it shows the form input of expiry date, else it hides it
any help would be appreciated. Thanks,
$(document).ready(function() {
$('#expiryDate_input').hide();
var allItems = {!! json_encode($items->toArray()) !!};
var arrayItemId = $('#item').val();
for (i=0; i < allItems.length; i++) {
var selectedItemid = allItems[i].id;
if (selectedItemid == arrayItemId){
if(allItems[i].expires) {
$('#expiryDate_input').show();
}
else {
$('#expiryDate_input').hide();
}
}
}
$("#item").change(function() {
var currentItemId = $(this).val();
if (currentItemId == '') {
$('#expiryDate_input').hide();
}
for (i=0; i < allItems.length; i++) {
var arrayItemId = allItems[i].id;
if (currentItemId == arrayItemId){
if(allItems[i].expires) {
$('#expiryDate_input').show();
}
else {
$('#expiryDate_input').hide();
}
}
}
});
});
function search(id) {
for (i=0; i < allItems.length; i++) {
var arrayItemId = allItems[i].id;
if (currentItemId == arrayItemId){
} return allItems[i];
else { return false
}
}
}
function showHide(item) {
if(item.expires) {
$('#expiryDate_input').show();
}
else {
$('#expiryDate_input').hide();
}
}
}
$(document).ready(function() {
function showHide(item) {
if(item.expires) {
$('#expiryDate_input').show();
}
else {
$('#expiryDate_input').hide();
}
}
function search(id) {
for (i=0; i < allItems.length; i++) {
var arrayItemId = allItems[i].id;
if (id == arrayItemId){
showHide( allItems[i]);
}
}
}
$('#expiryDate_input').hide();
var allItems = {!! json_encode($items->toArray()) !!};
var arrayItemId = $('#item').val();
search(arrayItemId);
}
$("#item").change(function() {
var currentItemId = $(this).val();
if (currentItemId == '') {
$('#expiryDate_input').hide();
}
search(currentItemId);
});
});
Related
How can I to delete div image and select the div.backgroundColor :)
for (let i = 0; i < color1.length; i++) {
color1[i].addEventListener('click', function (event) {
color1[i].style.backgroundImage = "none";
let colorItem = color1[i].style.backgroundColor;
console.log(some)
if (colorItem == "red") {
console.log("yes")
}
else {
console.log("no")
}
event.preventDefault();
});
}
I am trying to run this script which effectively goes through a json list that contains url and a size to go to the link set add the item to cart based on size, check if it is in stock or already added. I have got the link and the size searching algorithms work independently but together they seem to not work and I can't figure out why?
var foundall = false;
var copItems = [{
"url": "https://www.supremenewyork.com/mobile/#products/303518/22745",
"size": "Medium"
}];
for (var i = 0; i < copItems.length; i++) {
AddToCart(copItems[i], function(carted) {
console.log(carted);
});
}
function AddToCart(item, callback) {
location.href = item.url;
var counter = 0;
var waitToAppear = setInterval(function() {
if (document.querySelector('#cart-update > span')) {
if (document.querySelector('#cart-update > span').innerHTML == 'remove') {
return callback("failed");
clearInterval(waitToAppear);
} else if (document.querySelector('#cart-update > span').innerHTML == 'sold out') {
copSelectSize(size, function(data) {
return callback(data);
clearInterval(waitToAppear);
});
} else if (document.querySelector('#cart-update > span').innerHTML == 'add to basket') {
copSelectSize(item.size, function(Sized) {
return callback("failed");
clearInterval(waitToAppear);
})
} else {
counter += 1;
if (counter == 5) {
return callback("failed");
clearInterval(waitToAppear);
}
}
}
}, 100);
}
function copSelectSize(size, callback) {
var counter = 0;
var checkExist = setInterval(function() {
if (document.getElementById('size-options').length) {
var sizes = document.getElementById('size-options').options;
var size_id;
for (var i = 0; i < sizes.length; i++) {
if (sizes[i].innerText == '\(Size)') {
size_id = i;
document.getElementById('size-options').selectedIndex = size_id;
document.getElementById('size-options-link').innerHTML = '\(Size)';
if (document.querySelector('#cart-update > span').innerHTML != 'remove') {
document.querySelector('#cart-update > span').click();
return callback("success");
clearInterval(checkExist);
}
var checkExista = setInterval(function() {
if (document.querySelector('#cart-update > span').innerHTML == 'remove') {
checkExista = '';
}
clearInterval(checkExista);
}, 100);
break;
}
}
}
counter += 1;
if (counter == 5) {
return callback("failed");
clearInterval(checkExist);
}
}, 200);
}
When I input numbers into the array, I get two alerts: a "number" alert, and then an "undefined" alert. I don’t know how to fix this.
This is my JavaScript code:
var myStuff = [];
function myfunctionA() {
var enteredvalue = document.getElementById("numbers").value;
alert(typeof Number(document.getElementById('numbers').value));
if (enteredvalue == "") {
alert("Input is not a number");
} else if (isNaN(enteredvalue)) {
alert('You need to enter a valid number!');
}
var elementExists = false;
var x = document.getElementById('numbers').value;
for (var i = 0; i < myStuff.length; i++) {
if (myStuff[i] == Number(x)) {
elementExists = true;
}
}
if (elementExists != true) {
myStuff.push(Number(enteredvalue));
alert('Thank You for entering a valid number'. myStuff);
} else {
alert('Element is here');
}
}
function myfunctionB() {
window.alert(myStuff.length);
}
function myfunctionC() {
var sum = 0;
for (var i = 0; i < myStuff.length; i++) {
sum += myStuff[i];
}
alert(sum);
}
function myfunctionD() {
if (myStuff.length == 0) {
alert("already empty");
} else {
myStuff = [];
}
alert("Array Empty");
}
function myfunctionE() {
alert(myStuff.join('\n')); {
if (myStuff == [])
{
alert("Enter something into Array")
}
}
}
function bubbleSort() {
var sorted = true;
var temp;
while (sorted) {
sorted = false;
for (var i = 0; i < myStuff.length - 1; i++) {
if (myStuff[i] < myStuff[i + 1]) {
temp = myStuff[i];
myStuff[i] = myStuff[i + 1];
myStuff[i + 1] = temp;
sorted = true;
}
}
}
}
The following line evaluated to undefined:
alert('Thank You for entering a valid number'. myStuff);
Do you maybe mean?:
alert('Thank You for entering a valid number' + myStuff);
How can I save the remaining optionset items after removing an item from the list? Is there any other way of adding the removed option from an optionset in XRM?
function func1() {
var A1 = Xrm.Page.getAttribute("attribute1").getValue();
var B1 = Xrm.Page.getAttribute("attribute2").getValue();
var PickListA1 = Xrm.Page.getControl("attribute1");
var PickListA1Options = PickListA1.getAttribute().getOptions();
var j = 0;
if (B1 == "3") {
if (A1 == "1") {
PickListA1.clearOptions();
for (var i in PickListA1Options) {
if (PickListA1Options[i].value != "null") {
PickListA1.addOption(PickListA1Options[i]);
}
}
}
PickListA1.removeOption(1);
}
else {
PickListA1.getAttribute().setvalue(A1);
for (var i in PickListA1Options) {
if (PickListA1Options[i].value == "1") {
break;
}
else {
j++;
}
}
if (j > 1) {
PickListA1.addOption(PickListA1Options[1]);
}
}
}
Last week I created an e-shop with opencart. Now I'm trying to customize the default top menu. What I want to do is to keep the parent highlighted even when a child element is selected
I highlight the current page with this js :
<script type="text/javascript" >
function extractPageName(hrefString)
{
var arr = hrefString.split('/');
return (arr.length < 2) ? hrefString : arr[arr.length-2].toLowerCase() + arr[arr.length-1].toLowerCase();
}
function setActiveMenu(arr, crtPage)
{
for (var i=0; i < arr.length; i++)
{
if(extractPageName(arr[i].href) == crtPage)
{
if (arr[i].parentNode.tagName != "DIV")
{
arr[i].className = "current";
arr[i].parentNode.className = "current";
}
}
}
}
function setPage()
{
hrefString = document.location.href ? document.location.href :document.location ;
if (document.getElementById("menu") !=null)
setActiveMenu(document.getElementById("menu").getElementsByTagName("a"),extractPageName(hrefString));
}
window.onload=function()
{
setPage();
}
</script>
Any help would be greatly appreciated.
p.s. sorry for my English, if you can't understand what I want to make.
I figured it out. Tooraj gives me the idea. Here is my code :
<script type="text/javascript" >
function extractPageName(hrefString)
{
var arr = hrefString.split('/');
return (arr.length < 2) ? hrefString : arr[arr.length-2].toLowerCase() + arr[arr.length-1].toLowerCase();
}
function setActiveMenu(arr, crtPage)
{
for (var i=0; i < arr.length; i++)
{
if(extractPageName(arr[i].href) == crtPage)
{
if (arr[i].parentNode.parentNode.parentNode.parentNode.tagName != "DIV",
arr[i].parentNode.tagName != "DIV")
{
arr[i].className = "current";
arr[i].parentNode.parentNode.parentNode.parentNode.className = "current";
arr[i].parentNode= "current";
}
}
}
}
function setPage()
{
hrefString = document.location.href ? document.location.href :document.location ;
if (document.getElementById("menu") !=null)
setActiveMenu(document.getElementById("menu").getElementsByTagName("a"),extractPageName(hrefString));
}
window.onload=function()
{
setPage();
}
</script>