Istorn
Istorn

Reputation: 596

Modify HTML through JS gives me undefined (AngularJS error)

With this function:

function modifyJS(elemento){        
        if (document.getElementById(elemento).innerHTML.indexOf('✔')>0){
            document.getElementById(elemento).innerHTML=document.getElementById(elemento).innerHTML.substring(0,document.getElementById(elemento).innerHTML.length-29);
        }else{
            document.getElementById(elemento).innerHTML=document.getElementById(elemento).innerHTML+'<b color="green">&#10004;</b>';
        }
    }

I would like to modify the inherited HTML content. When I call the function the error is the following

TypeError: Cannot read property 'innerHTML' of nullangular.1.4.6.min.js:107

I breakpointed it, but the "elemento" variable passed to identify the HTML element is correctly given to the function.

Upvotes: 2

Views: 57

Answers (1)

Istorn
Istorn

Reputation: 596

Having this table:

 <table class="table table-bordered">
    <tr ng-repeat="x in menus" ng-class-even="'alt'">
    <td  style="background: white;" id="x.id" ng-click="addMenu(x.href,x.id)">{{cleanMenuAction(x.href)}}</td>

    </tr>
</table>

To modify the clicked "td" tag, in the end I modified the function as this:

//Funzione per aggiungere una voce di menu da abilitare per l'utente in creazione
$scope.addMenu= function(vocemenu,idmenu){

    if (vocemenu.indexOf(' SELEZIONATA')>0){
        vocemenu=vocemenu.substring(0,vocemenu.length-(' SELEZIONATA').length);
        $scope.cleanMenuAction(vocemenu);
        $scope.menus[idmenu-1].href=vocemenu;
    }else{
        $scope.cleanMenuAction(vocemenu);
        vocemenu=vocemenu+' SELEZIONATA';

        $scope.menus[idmenu-1].href=vocemenu;
    }
}

The second one, it was used to remove parts of string that I needn't:

//Funzione per rendere più leggibile la voce di menu da mostrare
$scope.cleanMenuAction= function(stringa){
    //Verifica se presente una &
    if (stringa.indexOf("&")>0){
        return (stringa.substring((stringa.indexOf('=')+1),stringa.indexOf('&')));
    }
    else{
        return (stringa.substring((stringa.indexOf('=')+1),stringa.length));
    }
}

Upvotes: 1

Related Questions