0

my page.php contains div id="container" with id="input" which default type is input type="text":

    <div class="container">  
        <div class="row">  
            <div class="col-75"> 
                <div id="container">
                    <input type="text" id="input" value="" name="password">
                </div>    
                <input type="submit" value="OK" id="logBtn">  
           </div> 
        </div> 
    </div> 

I want to change input type="text" to input type="password" for same input id="input" with receiving of value textToPassType(1):

function textToPassType(val1){
  if (val1 == 1){
     document.getElementById('input').type = 'password';
  } else if (val1 == 0){
     document.getElementById('input').type = 'text';
  }
}

This way works only if I call this function included to page.php, but if use it in external document: <script type="text/javascript" src="myjs.js"></script> method does not works. So, I'm looking for some correct method to change input type dynamically and keep same id name from external myjs.js to my page.php

9
  • Code works fine. So what are you doing wrong? How are you calling textToPassType Commented Aug 26, 2019 at 13:09
  • @epascarello Hello, I'm just calling it from one of if statement conditions, which console it true during typing to the same text-box textToPassType(1);, but it does not changes text to password, even if I call this function with load Commented Aug 26, 2019 at 13:18
  • well I ran the code in a snipplet and it ran on chrome. Commented Aug 26, 2019 at 13:22
  • @ epascarello yes, I see. and I can't find out what is wrong here, I have removed all css and focus caret function, but same result Commented Aug 26, 2019 at 13:25
  • jsfiddle.net/boqwgste runs fine for me Commented Aug 26, 2019 at 13:30

1 Answer 1

1

var is a keyword in JavaScript, used to declare a variable, you can't use it as a variable name, try naming your variable val or something:

function textToPassType(val) {
  if (val == 1) {
    document.getElementById('input').type = 'password';
  } else if (val == 0) {
    document.getElementById('input').type = 'text';
  }
}

textToPassType(1);
<div class="container">
  <div class="row">
    <div class="col-75">
      <div id="container">
        <input type="text" id="input" value="" name="password">
      </div>
      <input type="submit" value="OK" id="logBtn">
    </div>
  </div>
</div>

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

1 Comment

Hello, It was a typo with var, failure caused by something else, because I've missed main point, sorry for that, please check my edit, because I want change it from external myjs.js in page.php

Your Answer

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