Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahr's avatar
Level 10

How to add href in javascript?

I have two links mobile_numbe and site. When the user opens the site with a mobile phone, the link should change to the mobile number. When opened on a desktop, the link should be converted into a link address.

blade

<a id="contact" class="btn bg-red rounded-pill text-white btn-lg">Contact</a>

js

<script>
    let contact = href="tel:{{ $setting[0]->link ?? null }}";
    let link = href="https://example.com";

    let details = navigator.userAgent;
    let regexp = /android|iphone|kindle|ipad/i;
    let isMobileDevice = regexp.test(details);
    if (isMobileDevice) {
        document.getElementById("contact").innerHTML = contact;
    } else {
        document.getElementById("contact").innerHTML = link;
    }
    //
</script>
0 likes
6 replies

Please or to participate in this conversation.