How to access functions in javascript? [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Code:
<html>
<body>
<script>
function mmm(x){
var val= [10,20,30];
return val;
}
</script>
<script>
function my(){
var va= mmm();
var b= va[1];
document.write(b);
}
</script>
</body>
</html>
there is a mistake in the above code, output is not generated. What I need is to display contents of 'val' of 'mmm(x)' function inside 'my()' function using document.write().
The two functions should be in different script tags. What are the changes do I need to make?

That's because you are not calling my function anywhere.
Try this.
<html>
<body>
<script>
function mmm(x){
var val= [10,20,30];
return val;
}
</script>
<script>
function my(){
var va= mmm(1);
var b= va[1];
document.write(b);
}
</script>
</body>
<button onclick="my()">click</button>
</html>
What I have done here I have a page having a button, and when you will click on that button, it will call that my function, and you will get 20 on your page.

You are not calling the function my
<html>
<body>
<script type="text/javascript">
function mmm(x){
var val= [10,20,30];
return val;
}
</script>
<script type="text/javascript">
function my(){
var va= mmm();
var b= va[1];
document.write(b);
}
my();
</script>
</body>
</html>

Try this code:
<script>
function mmm(){ //unwanted parameter is removed
var val= [10,20,30];
return val;
}
</script>
<script>
function my(){
var va= mmm();
var b= va[1];
document.write(b);
}
my();//calling method here
</script>
DEMO

Related

i need help please check if anything is wrong with the program and please help me correct it [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
<!Doctype html>
<html>
<title>JavaScript Tutorial</title>
<body>
<script language = "javascript">
var cleanCities = ["Cheyenne ","Santa Fe ","Tucson ","Great Falls ","Honolulu"];
var cityToCheck = "Tucson";
if(cityToCheck === cleanCities[i]) {
alert("You are clean");
}
</script>
</body>
</html>
I cannot find the errors
it's a javascript code
<!Doctype html>
<html>
<title>JavaScript Tutorial</title>
<body>
<script language = "javascript">
var cleanCities = ["Cheyenne ","Santa Fe ","Tucson ","Great Falls ","Honolulu"];
var cityToCheck = "Tucson";
if(cityToCheck === cleanCities[i]) {
alert("You are clean");
}
</script>
</body>
</html>
It says there is some problem in line 14
Where is your for loop for which you're an index i? If its removed intentionally then Tucson in your array has a space in the end. Use trim method.
Also language = "javascript" is wrong, its usually type="text/javascript"
The code should be like below:
enter code here
var cleanCities = ["Cheyenne ","Santa Fe ","Tucson ","Great Falls ","Honolulu"];
var cityToCheck = "Tucson";
for(var i=0; i<cleanCities.length; i++) {
if(cityToCheck === cleanCities[i].trim()){
alert('you are clean');
}
}

Date Comparison Angular JS [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
var discover = $filter("date")(new Date('08-24-2015'), "MM/dd/yyyy");
var occur = $filter("date")(new Date('06-07-2018'), "MM/dd/yyyy")
console.log(discover);
console.log(occur);
if (discover < occur) {
console.log('Discover less than occur');
} else {
console.log('No');
}
The result is always No to whatever value I assign to discover variable.
Did I compare it wrong?
I would advise you to use momentJS for date-time operations. It is much cleaner to implement.
To check if a certain time is before or after another time you could use their query functions
moment('2010-10-20').isBefore('2010-12-31', 'year'); // false
moment('2010-10-20').isBefore('2011-01-01', 'year'); // true
// Code goes here
var app = angular.module("app", []);
app.controller("BaseController", function($scope,$filter) {
var discover = $filter("date")(new Date('05-24-2015'), "MM/dd/yyyy");
var occur = $filter("date")(new Date('06-07-2018'), "MM/dd/yyyy")
console.log(discover);
console.log(occur);
if (discover < occur) {
console.log('Discover less than occur');
} else {
console.log('No');
}
});
<!DOCTYPE html>
<html>
<head>
<script data-require="angularjs#1.5.5" data-semver="1.5.5" src="https://code.angularjs.org/1.5.5/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-app="app" ng-controller="BaseController">
</body>
</html>

Can't understand why my Javascript functions don't work? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to use some simple Javascript functions to change an <img>'s source to another picture when a button is pressed.
However, none of the buttons seem to work, not even the last one I made just to test whether I would get an alert.
I have tried figuring out what's wrong but I can't seem to find it. I have already searched similar questions and threads but the solution never applied to my case...
Thank you. (I only posted the <body></body> tags because there's nothing special in the <head> and co. tags)
<script type="text/javascript">
function myFunctionMice() {
document.getElementById("target").src = "http://www.clker.com/cliparts/b/Z/f/0/p/W/mouse-with-smaller-ears-md.png">;
};
function myFunctionLaser() {
document.getElementById("target").src = "http://www.clker.com/cliparts/b/Z/f/0/p/W/mouse-with-smaller-ears-md.png" >;
};
function myFunctionBirds() {
document.getElementById("target").src = "http://image.flaticon.com/icons/png/512/47/47080.png">;
};
function myTry() {
alert("example");
};
</script>
<h1 align="center">Web4Cats</h1>
<nav>
<button type="button" onclick="myFunctionBirds()">Birds</button>
<button type="button" onclick="myFunctionMice()">Mice</button>
<button type="button" onclick="myFunctionLaser()">Laser</button>
<button type="button" onclick="myTry()">Try</button>
</nav>
<img id="target" src="http://www.clker.com/cliparts/b/Z/f/0/p/W/mouse-with-smaller-ears-md.png">
You have a syntax error in your code. This >; needs to be removed. Otherwise your src is not valid. Look at the snippet below to see that your approach was good.
function myFunctionMice() {
document.getElementById("target").src = "http://www.clker.com/cliparts/b/Z/f/0/p/W/mouse-with-smaller-ears-md.png"
};
function myFunctionLaser() {
document.getElementById("target").src = "http://www.clker.com/cliparts/b/Z/f/0/p/W/mouse-with-smaller-ears-md.png"
};
function myFunctionBirds() {
document.getElementById("target").src = "http://image.flaticon.com/icons/png/512/47/47080.png"
};
function myTry() {
alert("example");
};
<h1 align="center">Web4Cats</h1>
<nav>
<button type="button" onclick="myFunctionBirds()">Birds</button>
<button type="button" onclick="myFunctionMice()">Mice</button>
<button type="button" onclick="myFunctionLaser()">Laser</button>
<button type="button" onclick="myTry()">Try</button>
</nav>
<img id="target" src="http://www.clker.com/cliparts/b/Z/f/0/p/W/mouse-with-smaller-ears-md.png">
the issues are 1.'>' must be removed after image url
2.your javascript code must be declared after the html code
<h1 align="center">Web4Cats</h1>
<nav>
<button type="button" onclick="myFunctionBirds()">Birds</button>
<button type="button" onclick="myFunctionMice()">Mice</button>
<button type="button" onclick="myFunctionLaser()">Laser</button>
<button type="button" onclick="myTry()">Try</button>
</nav>
<img id="target" src="http://www.clker.com/cliparts/b/Z/f/0/p/W/mouse-with-smaller-ears-md.png">
<script>
function myFunctionMice() {
document.getElementById("target").src = "http://www.clker.com/cliparts/b/Z/f/0/p/W/mouse-with-smaller-ears-md.png"
};
function myFunctionLaser() {
document.getElementById("target").src = "http://www.clker.com/cliparts/b/Z/f/0/p/W/mouse-with-smaller-ears-md.png"
};
function myFunctionBirds() {
document.getElementById("target").src = "http://image.flaticon.com/icons/png/512/47/47080.png"
};
function myTry() {
alert("example");
};
</script>
Your '>' characters cause an error at the end of your src definitions in your javascript functions.
ex :
document.getElementById("target").src = "http://image.flaticon.com/icons/png/512/47/47080.png" **>** ;
should be :
document.getElementById("target").src = "http://image.flaticon.com/icons/png/512/47/47080.png";
Do the same with every function in your code. it definitely works

Why is content written as text? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm trying to make a script that adds a link to a div in the onload and for some reason the content (images) is displayed as written html text. Why is that and how can I correct it?
<!DOCTYPE html>
<html>
<head>
<title></title>
<script language="javascript" type="text/javascript">
function AffiliateLink() {
var boxes = document.getElementsByClassName('AffiliateLink'),
i = boxes.length;
while (i--) {
var link = document.createElement('a');
var content = document.createTextNode(boxes[i].innerHTML);
link.setAttribute('href', boxes[i].getAttribute('title'));
link.appendChild(content);
boxes[i].innerHTML = '';
boxes[i].appendChild(link);
}
}
</script>
</head>
<body onload="AffiliateLink()">
<div class="AffiliateLink" title="http://www.google.com">Google</div>
<div class="AffiliateLink" title="http://www.apple.com"><img src="http://files.softicons.com/download/application-icons/black-icons-by-mike-demetriou/png/128x128/App%20apple%20logo.png"></a></div>
</body>
The document.createTextNode forces the HTML to be text. Just copy one innerHTML to the other.
http://jsfiddle.net/86fWM/1/
while (i--) {
var link = document.createElement('a');
link.setAttribute('href', boxes[i].getAttribute('title'));
link.innerHTML = boxes[i].innerHTML;
boxes[i].innerHTML = '';
boxes[i].appendChild(link);
}
Because that's explicitly what a textNode is that you're creating, it explicitly handles everything you give it as text and does not interpret the HTML. If you want to set HTML content, use the innerHTML attribute:
link.innerHTML = boxes[i].innerHTML;
It's debatable whether you may want to simply clone or move the boxes[i] node instead though instead of assigning one innerHTML to another.
Because you are creating a text node containing html tags, so they are treated as text and not html.
You can easily fix that by replacing
var link = document.createElement('a');
var content = document.createTextNode(boxes[i].innerHTML);
link.setAttribute('href', boxes[i].getAttribute('title'));
link.appendChild(content);
by
var link = document.createElement('a');
link.setAttribute('href', boxes[i].getAttribute('title'));
link.innerHTML = boxes[i].innerHTML;
More then one way to the target. the "create text node" made text node. replacing it with innerHTML will make the next code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function AffiliateLink() {
var boxes = document.getElementsByClassName('AffiliateLink'),
i = boxes.length;
while (i--) {
var a = document.createElement('a');
var content = boxes[i].innerHTML;
a.href = boxes[i].title;
a.innerHTML = content;
boxes[i].innerHTML = '';
console.log (i);
boxes[i].appendChild(a);
}
}
</script>
</head>
<body onload="AffiliateLink()">
<div class="AffiliateLink" title="http://www.google.com">Google</div>
<div class="AffiliateLink" title="http://www.apple.com"><img src="http://files.softicons.com/download/application-icons/black-icons-by-mike-demetriou/png/128x128/App%20apple%20logo.png"></a></div>
</body>
</html>

Convert HTML code into jquery or javascript? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a code in HTML using Spring notation.
The code is:
<c:if test="${testCSV==true}">
<img src="images/icons/edit.png" width="25" height="25" />
</c:if>
I want to check this condition in jQuery, So the condition checks dynamically.
please help me.
Add this code in html where you want to edit button to show
<a id="edit" href="#" onclick="editCSV(${command.id})" rel="tooltip-top">
<img src = "images/icons/edit.png" width = "25" height = "25" />
</a>
js
if (test == true) {
$('#edit').show();
} else {
$('#edit').hide();
}
pass true
<script type="text/javascript">
var test =pagetype('<%=${testCSV}%>');
if (test == true) {
$('#edit').show();
} else {
$('#edit').hide();
}
</script>
You can use this:
<script type="text/javascript">
var x = ${testCSV};
// some other code
</script>
The browser will see it as:
<script type="text/javascript">
var x = true; // or false
// some other code
</script>
Then, do whatever you like with it.

Categories

Resources