Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

check value is in array and compare with user agent

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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

688 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