Нужна помощь при вставке данных в HTML - PullRequest
0 голосов
/ 23 октября 2019

Я хочу вставить значение переменной Javascript в HTML-файл. Но вывод пуст. Нужные данные хранятся в переменной, но вывод в html-файле пуст.

Мой htmll-файл

<div class="student-info">
     <p>Name: Anon</p>
     <p>Registration Number:</p>

<script src="resources/js/data_inp.js">document.write(regno)</script>
<script src="resources/js/data_inp.js"></script>

Мой файл data_inp.js

//get regno
firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
        console.log(user);
        var email = firebase.auth().currentUser.email;
        var regno = email.substring(0, email.length - 8);
        //email is eg. 1234@xyz.com where 1234 is regno so I used this method to separate the regno
        console.log(regno);
    }
});

Чтоя делаю не так?

Ответы [ 2 ]

0 голосов
/ 23 октября 2019

В вашем HTML удалите script и сделайте span с id, затем нацельтесь на innerText диапазона.

<p>Registration Number: <span id="dynamic-value"></span></p>

Затем в вашем js,

document.getElementById('dynamic-value').innerText = regno
0 голосов
/ 23 октября 2019

Возможно, вы хотите сохранить весь javascript в одном файле и сохранить все html в одном файле и нацеливать на определенные элементы с помощью document.getElementById ()

html

<div class="student-info">
                        <p>Name: Anon</p>
                        <p id=registrationNumber>Registration Number:</p>
</div>
<script src="resources/js/data_inp.js"></script>

javascript

//get regno
firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
        console.log(user);
        var email = firebase.auth().currentUser.email;
        var regno = email.substring(0, email.length - 8);
        //email is eg. 1234@xyz.com where 1234 is regno so I used this method to separate the regno
        console.log(regno);

        var myElement = document.getElementById("registrationNumber");
        var newcontent = document.createElement('div');
        myElement.append(newcontent);
        newcontent.innerHTML = regno;
    }
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...