Solved

check value is in array and compare with user agent

Posted on 2013-01-03
1
225 Views
Last Modified: 2013-01-10
Hi,

I would like to know how to check an array in jquery to see if it contains a value?

var sitePrefList = { sitePrefs: [
				{ device: "mobile", value="1" },
				{ device: "desktop", value="2" }
			]
    };

Open in new window


I want to basically check if the user agent is mobile || desktop and then match it with the value in the array and display the value.

for (var i = 0; i < sitePrefList.sitePrefs.length; i++) {
            var sitePrefArray = new Array();
            sitePrefArray = sitePrefList.sitePrefs[i].id.split("||");
            var isCondMatch = false;
            for (var j = 0; j < sitePrefArray.length; j++) {
                  sitePrefQsDevice = sitePrefList.sitePrefs[i].device;
                  sitePrefQsValue = sitePrefList.sitePrefs[i].value;

            }
}

if (sitePrefQsValue != '') {
			changeSitePref(sitePrefQsValue);
			siteDetermined = true;
        }

Open in new window


I have a device check script which works ok and returns either mobile or desktop
function deviceCheck() {
    var deviceType = '';
    if (jQuery.browser.mobile) {
        deviceType = "mobile";
        return deviceType
    }
    else {
        deviceType = "desktop";
        return deviceType
    }
}

Open in new window

0
Comment
Question by:Al4ddin2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 38741392
function getValueForDevice() {
   var d = deviceCheck();
   for(var i=0;i<sitePrefList.sitePrefs.length;i++) if(sitePrefList.sitePrefs[i].device == d) return sitePrefList.sitePrefs[i].value;
   return null; // not found, return null
}

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question