Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

check value is in array and compare with user agent

Posted on 2013-01-03
1
Medium Priority
?
237 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
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 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)

972 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