0

I have a file named one.js and another file named two.js. There is a function named current() in one.js that returns a string that I would like to pass to two.js.

current():

function current()
{
   if(selection === 0 && yesResources[questionsCounter] != null)
   {
      return yesResources[questionsCounter];
   }
   else if(selection === 1 && noResources[questionsCounter] != null)
   {
      return noResources[questionsCounter];
   }
   else
   {
      return 'You are on the right track!';
   }
}

In an HTML file, I have the following script tags called:

<script type='text/javascript' src='one.js'></script>
<script type='text/javascript' src='two.js'></script>

In the HTML file, I've also written code with the purpose of changing the content of a modal box after a button click with the result of current(). The code is as follows:

<!-- Trigger/Open The Modal -->
<button id="myBtn">Resources</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">
          <span class="close">&times;</span>
      <h2>Resources</h2>
    </div>
    <div class="modal-body">
      <p>Test</p>
      <p id='modalContent'>current()</p>
    </div>
    <div class="modal-footer">
      <h3>Living Progress</h3>
    </div>
  </div>
</div>

The string that gets returned by current() is not displaying in the modal. Am I misunderstanding how to bring in a value from one JS to another?

2
  • That's not Javascript. Commented Aug 4, 2017 at 19:49
  • 1
    You have html code in two.js? Commented Aug 4, 2017 at 19:54

1 Answer 1

1

You are mixing javascript and html.

This part:

<!-- Trigger/Open The Modal -->
<button id="myBtn">Resources</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">
          <span class="close">&times;</span>
      <h2>Resources</h2>
    </div>
    <div class="modal-body">
      <p>Test</p>
      <p id='modalContent'>current()</p>
    </div>
    <div class="modal-footer">
      <h3>Living Progress</h3>
    </div>
  </div>
</div>

should be in HTML file, and you can change text in <p id='modalContent'></p> by calling document.getElementById("modalContent").textContent = current(); from javascript (make sure that DOM is ready when calling the function)

Sign up to request clarification or add additional context in comments.

3 Comments

That should be textContent, not innerHTML.
Oh ok, I see what you did. I made a mistake and said it was in a JS but I had that code in a HTML as you said (brain wasn't working haha). I made the same mistake thinking it was supposed to be .innerHTML, so I'll try .textContent
@zelitomas, I tried that and it did not seem to work. The button I set to open the modal on click is not triggering the modal anymore. Can you elaborate on what you mean by making sure the DOM is ready when calling the function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.