简体   繁体   中英

hide or show button in javascript

I am trying to hide or show a button in javascript, onload the button should be hidden.

function hideButton(){
    var x = document.getElementById('myDIV');
    x.style.display = 'none';
}

If data.nextPageURL has a string the button should be visable, if it does not have a string it should be hidden.

var x = document.getElementById('myDIV');
if(data.nextPageURL){
   x.style.display = 'block';
}
else if(data.nextPageURL == "") {
    x.style.display = 'none';
}

but don't know where I am going wrong.

Full ajax code:

function loadMore(url, data1) {
    $.ajax({
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        'type': 'POST',
        'url': url,
        'data': JSON.stringify(data1),
        'dataType': 'json',
        success: function (data) {
            //var json = JSON.stringify(data);
            var table = document.getElementById("searchList");
            for (var i=0; i < data.businesses.length ; i++) {
                var business = data.businesses[i];
                var row = table.insertRow(-1);
                var cell1 = row.insertCell(0);
                var cell2 = row.insertCell(1);
                var cell3 = row.insertCell(2);
                var cell4 = row.insertCell(3);
                var cell5 = row.insertCell(4);
            }
            var x = document.getElementById('myDIV');
                if(data.nextPageURL == ""){
                    x.style.display = 'none';
            }
            else {
                x.style.display = 'block';
            }

you have a logical issue in your code

var x = document.getElementById('myDIV');
if(data.nextPageURL){
   x.style.display = 'block';
} else {
    x.style.display = 'none';
}

If data.nextPageURL is here you show your button else you hide it.

Open the snippet and write anything inside the nextPageURL , then the div will appear.

 var x = document.getElementById('myDIV'); function hideButton() { if (data.nextPageUR) { x.style.display = 'block'; } else { x.style.display = 'none'; } } hideButton(); 
 #myDIV { height: 50px; width: 50px; background: blue; } 
 <div id='myDIV'></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM