Solved

check value is in array and compare with user agent

Posted on 2013-01-03
1
219 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
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help with jQuery plugin 10 28
Can't get js include to work in html file 8 13
tiny glitch in my main slider 3 24
Jquery 2 11
I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

773 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