Solved

check value is in array and compare with user agent

Posted on 2013-01-03
1
213 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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now