Passing variable to function in javascript and jQuery - javascript

quick question,why this don`t work and how to fix it
$("#Button").click(function(){
count++;
var b=new Button(count);
b.render();
$("#div"+ count).dblclick(function(){
var diva=this;
$(this).append("<span id='colorchange'><input type='color' id='color' onchange='func("+diva+")' name='favcolor'></span>");
});
});
function func(diva){
alert(diva);
}
i`m getting error Uncaught SyntaxError: Unexpected identifier

You can add jquery library "http://code.jquery.com/jquery-1.9.1.js",
and Button is not recognized unless you defined it anywhere...
You can get help from "http://sideroad.secret.jp/plugins/jQueryRender/"

Related

Getting a TypeError (not a function) Jquery/Javascript

I'm trying to make a super simple "see more.." link. I was doing it before with a button, but it didn't work very well on mobile, so I've changed it to a link, but I'm getting this error:
jquery-3.1.1.min.js:2 jQuery.Deferred exception: seeMore is not a function TypeError: seeMore is not a function
Here's the code. I reckon it's just a stupid syntax error somewhere.
$(document).ready(function(){
var seeMore = document.getElementById('seeMore');
seeMore.onclick=seeMore();
function seeMore(){
document.getElementById('infoText').innerHTML= 'More text';
};
function seeLess(){
document.getElementById('infoText').innerHTML= 'Less text';
};
});
and here's the linked seeMore ID.
See More
What's going on here??
You use the same name for a property and a function and therefore its not clear what you mean.
You could rename one variable.
Also when you assign seeMore() with the brackets you assign the return value of the function and not the function itself. You need to remove the brackets.
You could also use the events which jquery provide to easily support all browsers.
$(document).ready(function(){
var seeMoreElement = document.getElementById('seeMore');
function seeMore(){
document.getElementById('infoText').innerHTML= 'More text';
};
function seeLess(){
document.getElementById('infoText').innerHTML= 'Less text';
};
$(seeMoreElement).click(seeMore);
});

How to define a jQuery function properly

I want to write an jquery function because otherwise I have to write some code over and over.
Here is wat the function was first:
$(".checkbox-car").click(function(){
$(this).toggleClass('checked-car')
});
$(".checkbox-bus").click(function(){
$(this).toggleClass('checked-bus')
});
And this is what I tried to do with my own function:
$.fn.checkedFunction = function(Clicked, Checked){
$(this).click(function(){
console.log('check');
$(Clicked).toggleClass(Checked);
});
}
$('.checkbox-car').checkedFunction('.checkbox-car','.checked-car');
But I get the error that the checkedFunction is not defined.
What am I doing wrong here can someone help me out?
You're getting an error that checkedFunction isn't defined because it isn't. checkedFunction is a property defined in$.fn object.
To use the function you created, you should do
$(".some-element").checkedFunction(...args).
More over, you should read the jQuery docs.
Just had to do this:
$.fn.checkedFunction = function(Clicked, Checked){
$(this).click(function(){
$(Clicked).toggleClass(Checked);
});
}
$('.checkbox-car').checkedFunction('.checkbox-car','checked-car');
$('.checkbox-bus').checkedFunction('.checkbox-bus','checked-bus');
remove the dot before the second checked- class

Uncaught ReferenceError: calcDollar is not defined

[I've been getting undefined error and I dont know how to fix it.
Here's my code]1
You have Typo in your code
The function you have defined in js.js file is clacDollar
And the function you are calling in 6.html is calcDollar
if you use calcDollar instead of clacDollar in your code is better ..
try using
function calcDollar () {
in your definition

Uncaught Error: Syntax error, unrecognized expression: #[object Object]

The code I am getting the error for, after doing research I can not understand why this error keeps coming up. The line of code was working before but now this is crashing my entire project.
function HomePageView_GetActiveTopicParentId(id) {
return $("#" + id).attr("egain-parent-identifier");
}
the second set of code
function HomePageController_TopicsViewChanged(id) {
sessionStorage.setItem(CURRENT_TOPIC_PARENT_ID, HomePageView_GetActiveTopicParentId(id));
sessionStorage.setItem(CURRENT_TOPIC_ID, HomePageView_GetActiveTopic(id));
$.mobile.changePage("#ViewTopicPage");
}
The problem is that id needs to be a string but it is some other type of object. Where is your HomePageController_TopicsViewChanged function being called, and what is being passed in as the id parameter?

cannot get simple appendChild function working

I am testing javascript code on W3schools' site and I cannot get the appendChild function working.
Hope its not a dumb error.
here it is:
<script type="text/javascript">
function append()
{
var node = document.getElementById(“history”);
node.appendChild(document.createTextNode("text"));
}
window.onload = append;
</script>
<div id="history"></div>
You don't have proper double quotes (I don't know what the others are called):
document.getElementById(“history”);
// ^ ^
This throws the error:
Uncaught SyntaxError: Unexpected token ILLEGAL
It works with:
document.getElementById("history");
DEMO
OT: w3schools is not a good resource for learning (http://w3fools.com/). Better have a look at the Mozilla documentation: https://developer.mozilla.org/en/javascript

Categories