I have a handful of js scripts in jquery:
vars.js
$(function(){
function yesOrNo(val){
if (val == "yes"){
return "yes";
}
if (val == "no"){
return "no";
}
});
scripts1.js
/// include(vars.js) <-- what would go here?
$(function(){
var var1 = "yes";
var test1 = yesOrNo(var1);
alert(test1);
});
scripts2.js
/// include(vars.js) <-- what would go here?
$(function(){
var var2 = "no";
var test2 = yesOrNo(var2);
alert(test2);
});
scripts3.js
/// include(vars.js) <-- what would go here?
$(function(){
var var3 = "yes";
var test3 = yesOrNo(var3);
alert(test3);
});
How can i call the function yesOrNo from a different scripts.js page?
If you want to add another file add type="module" parameters in your main code, then add the line import {yesOrNo} from './vars.js'; in your code.
Then, in the vars.js file, add the function as export function yerOrNo, not function yesOrNo.
My edit of your code:
vars.js
export function yesOrNo(val){
if (val == "yes"){
return "yes";
}
if (val == "no"){
return "no";
}
}
main.js
import {yesOrNo} from './vars.js'
$(function(){
var var2 = "no";
var test2 = yesOrNo(var2);
alert(test2);
});
On your html call main.js like that
<script src="main.js" type="module"></script>
Related
</script>
<button onclick="password1()">Password</button>
<script>
function password1(){
var pass = prompt ("Password"); -- Pasword Prompt
var wo = "No"; -- If Wrong
var rd = "Yes"; -- If Correct
var awn = 123; -- Code
if (pass == 123) { -- Check the password
document.write(rd);
}
}
</script>
This is what i tried but for a reason nothing appears Please help me
Wrong comments
No "else" statement
Variable "awn" defined but never used
<script>
function password1(){
var pass = prompt("Password"); // Pasword Prompt
var wo = "No"; // If Wrong
var rd = "Yes"; // If Correct
var awn = 123; // Code
if (pass == awn) { // Check the password
document.write(rd);
} else {
document.write(wo)
}
}
</script>
Is there any way to search a specific part on my input with Javascript/jQuery?
Tried to do with 2 different ways, but no results for that
<script type="text/javascript">
$("#button").click(function () {
$("#DivToToggle").toggle();
var campo = document.getElementById('FieldToSearch');
if (campo.contains("Test")) {
document.getElementById('FieldToWrite').value = "123";
}
else {
document.getElementById('FieldToWrite').value = "456";
}
});
and
<script type="text/javascript">
$("#button").click(function () {
$("#DivToToggle").toggle();
var field = document.getElementById('FieldToSearch');
if (field.match(/Test.*/)) {
document.getElementById('FieldToWrite').value = "123";
}
else {
document.getElementById('FieldToWrite').value = "456";
}
});
#EDIT
Thanks to #TalhaAbrar, found the correct way to do it.
<script type="text/javascript">
$("#button").click(function () {
var field = document.getElementById('FieldToSearch').value;
if (field.indexOf("Test")) {
$("#DivToToggle").toggle();
document.getElementById('FieldToWrite').value = "123";
}
else {
$("#DivToToggle").toggle();
document.getElementById('FieldToWrite').value = "456";
}
});
Try to fetch the value of search field like document.getElementById('FieldToSearch').value and then use function indexOf in your if statement, that should work for you.
example code:
var field = document.getElementById('FieldToSearch').value;
if (field.indexOf("value_to_check")) {
// true if exists
} else {
// false if doesn't
}
I think you need to check the value of the field, e.g.
var campo = document.getElementById('FieldToSearch').value;
...
And then use includes(), e.g.
if (campo.includes("Test")) {
...
I'm creating a script to check something in my code. This script runs successfully on Chrome, but it doesn't run on IE.
This is run on onsubmit in my form
<form id="frm1" name="frm1" method="post" onsubmit="return checkForm();" action="save_dept_add.php">
And this is my script (I've put it in head tag)
<script language="javascript" type="text/javascript">
function checkForm()
{
if (!window.console) {
console = { log: function(){} };
}
var e = document.getElementById("dept");
var xtext = e.options[e.selectedIndex].text;
if(xtext == ""){
console.log("no dept");
alert("Please select your department");
return false;
}else{
console.log(xtext);
var q = document.getElementById("quantity");
var check = q.value;
console.log(check);
if(check == ""){
alert("Please insert a quantity");
return false;
}else{
return true;
}
}
}
Can anyone tell me what is wrong?
Add this to the top of your JavaScript file
if (!window.console) {
console = { log: function(){} };
}
I am trying to use JavaScript to Make Focus on Text Box.But It does't works,Please tell the way to Using focus() and set null value for that text Box.
Script:
<script type="text/javascript">
$(document).ready(function ()
{
var str = ('#ViewData["ControlView"]'); //alert(str);
if (str == "1")
ShowProduct();
else
ShowGrid();
});
var message = ('#ViewData["Success"]');
if (message == "Product Code Already Exits.")
{
document.getElementById("Item_Code").value ="";
document.getElementById("Item_Code").focus();
}
View:
#Html.TextBox("Item_Code", "", new { #Maxlength = "10", id = "Item_Code" });
The code for focus() in Javascript is:
var message="message";
if(message=="")
{
document.getElementById("Item_Code").focus();
document.getElementById("Item_Code").value=="";
}
Jquery solution
<script type="text/javascript">
$(document).ready(function ()
{
var str = ('#ViewData["ControlView"]'); //alert(str);
if (str == "1")
ShowProduct();
else
ShowGrid();
});
var message = ('#ViewData["Success"]');
if (message == "Product Code Already Exits.")
{
$("#Item_Code").val("");
$("#Item_Code").focus();
}
Try using jQuery it's easier and clean
<script type="text/javascript">
$(document).ready(function () {
$(function () {
$('#Item_Code').focus();
});
});
</script>
In this script block
<script type="text/javascript">
$(document).ready(function ()
{
var str = ('#ViewData["ControlView"]'); //alert(str);
if (str == "1")
ShowProduct();
else
ShowGrid();
});
var message = ('#ViewData["Success"]');
if (message == "Product Code Already Exits.")
{
document.getElementById("Item_Code").value ="";
document.getElementById("Item_Code").focus();
}
declaration and using message variable and document.getElementById function call not in ready function, so if declaration of yours textbox after this then in call document.getElementById("Item_Code") return null, because element with this id has not yet been rendered
for solve put code
var message = ('#ViewData["Success"]');
if (message == "Product Code Already Exits.")
{
document.getElementById("Item_Code").value ="";
document.getElementById("Item_Code").focus();
}
in ready function like this:
$(document).ready(function ()
{
var str = ('#ViewData["ControlView"]'); //alert(str);
if (str == "1")
ShowProduct();
else
ShowGrid();
var message = ('#ViewData["Success"]');
if (message == "Product Code Already Exits.")
{
document.getElementById("Item_Code").value ="";
document.getElementById("Item_Code").focus();
}
});
my javascript function is throwing this error
var cycles = 0;
var exec_next = null;
function timer_interrupt() {
cycles++;
if (exec_next) {
var cmd = exec_next;
exec_next = null;
cmd();
}
}
using firebug it says the } should be at the end of the cycles++; what to do? I don't have any xml script being displayed?
it is being included in my template like this
<script language="JavaScript" src="mysript.js">
</script>