0

I have two javascript function in my index.js file saved in the same folder directory as my html file but for some reasons i am able to call only one of the function the second function is not getting called.

index.js

function cond(){
    alert('Is the form correct');
}

function isNumber(e){
    e = e || window.event;
    var charCode = e.which ? e.which : e.keyCode;
    return /\d/.test(String.fromCharCode(charCode));
}

index.html

<?php require "header.html"?>

<label>Year</label>
<input type ="number" min="1950" max="2050" name="year" onkeypress="return isNumber(event)" required>

<label>school</label>
<input type ="text" onclick="cond();" name="school" required>

header.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <script src="index.js"></script>
    </head>
    <body>
    <main>

Am able to use the isNumber() function successfully but the other function cond() is not getting called when the text is clicked

1
  • Not related to your question, but you are including partial header for every page. It might be better to define layout.html with included content instead with require. Commented Mar 23, 2022 at 6:17

2 Answers 2

1

Maybe cond is shadowed by other function? Try changing it's name. Or take a look at the console output.

Usually it is a good practice to retrieve element and then bind event listener to it. Now your HTML is coupled with the JS. Here is a working example:

https://jsfiddle.net/nr65sLc0/

var schoolInput = document.getElementById('school-input');
if (schoolInput) {
  schoolInput.addEventListener('click', cond);
}

Remember to add id attribute for each input.

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

Comments

0

In your index.html file, you can't use PHP ( you required a file ) because index.html extension is HTML at first you should change that to PHP then both of them will work correctly.

Change index.html to index.php

5 Comments

sorry that was a mistake in the question the file name is actually index.php but it still not working even with that
So Do you have another file that you've included it in you file? because both functions are working for me. Please take a look at your console. Is there any error?
Uncaught ReferenceError: cond is not defined this the error from the console
I didn't get such an error, maybe you have a problem adding files. Why don't you put your codes ( header.html & index.php ) in a PHP file? and another thing is that the <main> tag in header.html doesn't have closed tag.
i have the closing tag for the <main> in another html file still the same issue but never mind i guess i just have to used the script in the index file directly I was only trying to clean up the code a bit.Thanks anyways

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.