How To Create Argument Defaults For Function
Mar 2, 2010
How can I create argument defaults for a function? I have this function (the purpose of the lines I included were to set defaults if the user forget to input something simply to prevent an error.
Code:
addHSLink = function (hsElem, hsUrl, hsType, hsTitle, hsWidth, hsHeight) { //hsElem[string], hsUrl[string], hsType[string], hsTitle[string], hsWidth[string/number], hsHeight[string/number]
var jQElem = $(hsElem);
if (typeof hsUrl !== 'string') {
hsUrl = '[URL]';
} if (typeof hsTitle !== 'string') {
hsTitle = '';
} if ((typeof hsWidth !== 'string' && isNaN(parseFloat(hsWidth))) || hsWidth !== 'number') {
hsWidth = 800;
} if ((typeof hsHeight !== 'string' && isNaN(parseFloat(hsHeight))) || hsHeight !== 'number') {
hsHeight = 600;
}
//Some other code
}
I would like to save some programming space and time by finding another way to set defaults. I saw somewhere that you could set a default in the arguments list
Code:
function someFunction(aVariable = false) {
//some code
}
But when I try to do this with all my arguments, I get the error:
"Missing ) after formal parameters".
View 4 Replies
ADVERTISEMENT
Nov 30, 2006
how to you use the result of a function as the argument for another function instead of passing the actual function to it. i.e.
function foo2(){}
function foo(func){}
foo(foo2);
This passes foo2 to foo, can I do foo(foo2()) ? i.e. is the difference between whether a function is passed or evaluated dependent on whether you use "()" after the function name?
View 1 Replies
View Related
Mar 20, 2009
I am trying to design menu and also been successful at some extent. Root menu items doesn't create any problem but when I used following code to create drop down submenu items i got the error:Invalid argument
The error causing line is bold
While this javascript code doesn't work in FF3.x.x but in IE.
View 2 Replies
View Related
Jul 12, 2011
I am facing a problem in passing replace() function as an argument in user defined java function.
intention is to pass a file path to my user defined function, but before passing the path i want to replace the character '' to '\'
I am posting my javascript function here:
<a href="#" onclick="OpenDocPreview('<%# Eval("PATH")%>'.replace(/\/g,"\\"), '<%# Eval("Filename")%>')"><%# Eval("DocTitle") %></a>
function OpenDocPreview(url, docname) {
alert('message from search base : ' + url + ' ' + docname);
}
View 1 Replies
View Related
Jul 12, 2011
I am facing a problem in passing replace() function as an argument in user defined java function
intention is to pass a file path to my user defined function, but before passing the path i want to replace the character '' to '\'
I am posting my javascript function here:
<a href="#" onclick="OpenDocPreview('<%# Eval("PATH")%>'.replace(/\/g,"\\"), '<%# Eval("Filename")%>')"><%# Eval("DocTitle") %></a>
function OpenDocPreview(url, docname) {
alert('message from search base : ' + url + ' ' + docname);
}
View 4 Replies
View Related
Mar 14, 2002
How do you pass an argument to a function.
for example if you have a <span id = "test" onMouseOver ="myfunc(x)>
eg you have lots of differant span id's but you want then all to have a certain background-color with each individual on mouseover event without having to write separate code for each span id
eg <span id=" test2"onmouseover ="this.style.background ='red'>....
View 1 Replies
View Related
Jun 9, 2007
How can I modify any one of these global variable identified via a function
argument?
var x1 = "bla";
var x2 = "bla";
var x3 = "bla";
function modify(variable) {
???? [variable]???? = "blabla":
}
The following onclick should change the string value of variable 'x1' from
"bla" to "blabla", via the above modify() function.
<a ... onclick="modify('x1')">
View 8 Replies
View Related
Jun 23, 2009
I have used an object in the past as a function argument, but this was for a plugin that I wrote. Using it in the architecture of a plugin it worked. BUT, this time, I just want to write a normal function, but still use an object to set defaults and pass in changes to those defaults through as a param. Is this possible, or do I have to make this a jQuery function like $.myfunction() ?
[Code]...
View 5 Replies
View Related
Mar 12, 2009
I'm trying to pass an array as an argument for a function on load of an html page, but can't seem to get it to work. Here is my code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
[code]....
View 2 Replies
View Related
Feb 8, 2011
What syntax do I use to pass the div_name argument value to the rotate function in the setInterval function? I can't seem to get it to work.
Code JavaScript:
function theRotator(div_name) {
//Set the opacity of all images to 0
[code]....
View 2 Replies
View Related
Aug 3, 2011
How I use $.ajax() for passed argument to php function other file example index.php.
View 1 Replies
View Related
Dec 1, 2011
New to JS. Have ordered some new books, but need to get somethings going in the mean time. What I wanted to do is to link to a new page having a date range input in the form of 6 text boxes, 2 sets of mm-dd-yy, from and to, where the upon loading the page box 1 of the from would auto focus and then auto tab and then post where php could take over on the server side. Setting up the form and the lay out, no problem. Auto focusing went just like expected using the following code, included because on the next step everything fell apart.
Code:
<script type="text/javascript" >
window.onload=function() {
document.getElementById("input1").focus();
}
</script>
So then the wheels came off and in response, began to simplify what was trying to be done to find where the issues were. As far as I can get working is:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL]">
<html>
<head>
<script type="text/javascript" >
window.onload=function() {
document.getElementById("input1").focus();
}
</script>
<script type="text/javascript" >
function myFunc() {
alert("is this working");
}
</script>
</head>
<body>
<input id="input1" name="input1" type="text" onkeyup="myFunc()" maxlength="2" value="type a No." />
</body>
</html>
First issue with this: When I first get to the page everything is just as ordered. The focus is on the text box and the default value is "type a No." When I press a key onkeyup, I bring up the alert box with "is this working" in it. But when I refresh the page with the refresh button, after clicking the "ok" on the alert box, the character that was typed in is still displayed. If I use the link to the page, or the URL from the address bar, then the page reloads properly with the default value, "type a No".
How do you get the default textbox value using the refresh button as apposed to reloading the page using a link or from the address bar? Using Firefox 8.0 more important at the moment: If I change the script to pass an argument to the function the script crashes. It does not pull up an alert box and freezes. My guess is that I'm doing something wrong on a concept level. Changed the default value of the text box to " ". Looked like to me there was an issue with the size specification and the default string length as you could only enter in a key stroke by highlighting the text and replacing it.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL]">
<html>
<head>
<script type="text/javascript" >
window.onload=function() {
document.getElementById("input1").focus();
}
</script>
<script type="text/javascript" >
function myFunc(awe) {
alert(awe);
}
</script>
</head>
<body>
<input id="input1" name="input1" type="text" onkeyup="myFunc("123")" maxlength="2" value="" />
</body>
</html>
View 1 Replies
View Related
Feb 22, 2011
I have written the following code (quite meaningless. Just to check why setTimeout is not working in a similar real-life code) to enable the user to input a given time interval (hh:mm:ss) when a p is clicked, and then alerting the user with the time entered in the seconds portion one second after the div is clicked. But it is not working. I think the setTimeout is the culprit, the way I am passing parameters to the function inside it, but don't know where exactly am I erring.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
[Code]....
View 3 Replies
View Related
Oct 21, 2009
All the function extending I see extends on an _object_. I want to extend on a string.
/**
* Returns a slug version of a string
*/
jQuery.extend ({
slug: function( text ) {
return text.trim().toLowerCase().replace( /[^-a-zA-Z0-9s]/g,'' ).replace( /[s]/g, '-' );
}});
I want to be able to do something like:
$('#form_element').val().slug();
The above does not work. I even tried modifying the 'trim' function in jQuery core to include the rest of the enhancements, but it didn't work.
View 2 Replies
View Related
Dec 21, 2010
I am new to Jquery and would like to know, when we write a Jquery function and pass 'e' or any other word or letter as first argument, what does it represent? Also If I want to pass some parameter to my function along with 'e', how can I pass it?
View 1 Replies
View Related
Dec 1, 2011
What I wanted to do is to link to a new page having a date range input in the form of 6 text boxes, 2 sets of mm-dd-yy, from and to, where the upon loading the page box 1 of the from would auto focus and then auto tab and then post where php could take over on the server side. Setting up the form and the lay out, no problem. Auto focusing went just like expected using the following code, included because on the next step everything fell apart.
[/CODE]
<script type="text/javascript" >
window.onload=function() {
document.getElementById("input1").focus();
[Code].....
View 3 Replies
View Related
Dec 8, 2006
Given the following code:
var xf2;
function f1()
{
if (document.createElement && document.body && document.body.appendChild) {
xf2 = new f2(arguments[]);
}
}
I am getting a syntax error in the "new" statement following the [].
How do I pass the variable-length arguments list received by f1 on to f2?
View 4 Replies
View Related
Oct 5, 2010
I'm trying to pass titleArray and pointsArray to the task(); I'm getting an error mgs this.assignments() is not a function. I've highlighted this.assignments()
[Code]...
View 6 Replies
View Related
Apr 20, 2011
I have a simple function to display an image when called, but if I try to rewrite the function to take the image as an argument, it stops working.
works:
<html>
<script language="JavaScript">
var ImagePlusSign = '<img src="plus.jpg" name="slide" width="65" height="50"/>';
[code]...
View 3 Replies
View Related
Apr 20, 2009
How do i use a function to find the average of the values in an array of numbers passed as argument to a function.(using java script) thank you for your help...
View 1 Replies
View Related
May 10, 2011
I am trying to pass an argument to a function to process server responses. My original line of code looked like this: [url]
This works well enough as is, but I am trying to pass arguments to the function, and everything I am reading says to contruct the code like this: [url]
Every example I have come across uses this same approach, but I keep getting the error "Expected ';'" pointing to this line and then the code fails to execute. I just cannot see where there is anything missing, and it looks pretty much identical to my examples.
I have also tried replacing the call to the function with the actual code in long-form, and the result is the same.
View 3 Replies
View Related
Aug 25, 2010
I use jQuery for loading ajax content, changing css etc. I want to make button that does something similar to "Refresh" - changes back all css changes to defaults.
Is there a function that could do this?
View 2 Replies
View Related
Oct 24, 2009
There appears to be no difference between these two when the page loads.
<select id="one">
If there is no "selected" then it always defaults to first value.
How can I tell if the page has loaded and no options have been selected?
View 2 Replies
View Related
Oct 23, 2010
I have a form and am calling the insert into page using ajax. However when I click on submit my welcome page, set as my default page is called.
Looking around I think possibly a 401 is being generated by my Header and so it is being set to my default page.
So I guess my questions are:
1. Is this the most likely reason or when you look at my script does something else spring to mind?
2. How would I test to see if I am right (I tried getHeader but I don't think I understood how to set it out properly)
3. How would I resolve this.
My code is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html>
<head>
[Code]....
View 6 Replies
View Related
Jun 29, 2010
I have a long form and I have some checkboxes that do a onclick and show and hide the sub forms. This all works just fine but i want to onclick to also load some defaults into some of the subforms. Most of the fields in the sub form are drop down boxes. Below is my code to hide and unhide the tables I would like to just add to this code so I can just pass it a variable to change the defaults of said table.
function check(tableId, ctrl){
document.getElementById(tableId).style.display = (ctrl.checked) ?
"":"none";
}
If I make no sense on what I am asking for I will try to explain more.
View 14 Replies
View Related
May 15, 2009
My jQuery-Plugin shoud produce two buttons with a onclick-event.Problem:The onclick-event got fired, when I call the plugin, without clickingon the button.Why got the click-event fired without clicking the button?Here my (shorted) plugin:
(function($) {
$.fn.rte = function(options) {
$.fn.rte.defaults = {
[code]....
View 2 Replies
View Related