Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change Color for iPhone visitor

How can i change the color of an element, depending if the visitor is using ios (iPhone)?

This is what i tried:

<p id="TestID">Change this color for iPhone</p>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

document.onload = function myFunc(){

if( (navigator.platform.indexOf("iPhone") != -1)) 

     document.getElementById("TestID").style.color = "red !important";);}

But it did not work.

Any suggestions?

Much appreciated

>Solution :

here how i check for users’ device. maybe this works for you too.

const userAgent = navigator.userAgent || navigator.vendor || window.opera;
if (/android/i.test(userAgent)) {
    // android users
} else if (/iphone/i.test(userAgent) || /ipad/i.test(userAgent)) {
    // ios users
}

for your case, try:

window.onload = () => {
    const userAgent = navigator.userAgent || navigator.vendor || window.opera;
    if (/iphone/i.test(userAgent) || /ipad/i.test(userAgent)) {
        document.querySelector("#TestID").style.color = "red";
    }
}

also you used document.onload incorrectly. more on that

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading