Home > database >  js does not work on mobile but on my computer
js does not work on mobile but on my computer

Time:05-19

I was building an AJAX search system and on my computer everything worked fine, but then I tried it on my mobile, and it didn't work. Then I tried console.log and document write those work, but other things didn't.

const btn = document.createElement('button')
btn.innerText = 'hallo'
btn.id = 'test'

document.getElementById('content').append(btn)

const btnEl = document.getElementById('test')
btnEl.addEventListener('click', ()=>
{
    alert('hallo')
})

I build this simple button with JS to test if this would work and on my computer it worked perfectly fine but on mobile the button is not even displayed

here is a part of the AJAX code

suche = ()=>
{

    let xhr = new XMLHttpRequest()
    this.daten = [];

    let produkteEntfernen = ()=>
    {
        //removes already displayed products
    }

    let displayProdukte = ()=>
    {
        //formats the data to html and displays it
    }

    xhr.onreadystatechange = ()=>
    {
        if (xhr.status === 200 && xhr.readyState === 4)
        {
            this.daten = JSON.parse(xhr.response)
            produkteEntfernen()
            displayProdukte()
        }
    }

    buildRequestUrl = ()=>
    {
        //returns the url to send to 
    }

    xhr.open('GET', buildRequestUrl())
    xhr.send()
}

// the name id is from an input field of type text
let shopSuche = document.getElementById('name')

shopSuche.addEventListener('keyup', ()=>
{
    suche()
})

Does someone know a solution or know what I do wrong?

CodePudding user response:

The append method is fairly new (although browser compatibility for it is quite good, even on mobile).

For older browsers, you may need to use appendChild instead. In the code above, you literally just have to change the method name to do that. (append allows more kinds of arguments than appendChild does, but appendChild supports what you have there.)

CodePudding user response:

is the JavaScript enabled on your phone? did you try another browser like chrome?

  • Related