Pass parameter to a script function - javascript

I'm a beginner on this so please be patient with me. I have look up this problem and did not found a solution.
I have the following code:
on the head
<script>
function ledonoff(led)
{
if (document.getElementById('ck1').checked == true){
document.getElementById('led1').style.backgroundImage="url('Image/led_green.gif')";}
else {
document.getElementById('led1').style.backgroundImage="url('Image/led_red.gif');}
}
</script>
on the body
<div id="led1"></div>
<input name="" type="checkbox" id="ck1" onchange="ledonoff('led1')" value=""/>
I will like to have the function ledonoff take 2 parameters to be used in function : div id to be changed
and checkbox that is to be verified on the if of the function.
Can anyone help me with this.
Thank you

change the function to:
function ledonoff(led,checkBox){
var theDiv = document.getElementById(led),
check = document.getElementById(checkBox);
if (check.checked == true){
theDiv.style.backgroundImage="url('Image/led_green.gif')";
} else {
theDiv.style.backgroundImage="url('Image/led_red.gif')";
}
}
and the htmlonchange to:
"ledonoff('led1','ck1')"
in jsbin

Modify your code to:
<script>
function ledonoff(isChecked, elementId)
{
if (isCkecked){
document.getElementById(elementId).style.backgroundImage="url('Image/led_green.gif')";}
else {
document.getElementById(elementId).style.backgroundImage="url('Image/led_red.gif');}
}
</script>
And The body to:
<input name="" type="checkbox" id="ck1" onchange="ledonoff(this.checked, "led1")" value=""/>Led 1

You can simply pass it: onchange="ledonoff('led1', 'ck1')".
However your function is expecting one argument, so you need to amend that.
function ledonoff(led) -> function ledonoff(led, ck)
Just to note
This type of JavaScript is called Obtrusive Javascript. The recent shift in design principle is to have your Javascript and your HTML in completely separate files. Instead of assigning functions to events in an attribute fashion, you should use the DOM to add event listeners to DOM Elements. Here is a good tutorial to get you started on the idea.

do this:
<script>
function ledonoff(led,checkbox){
if(document.getElementById(checkbox).checked == true){
document.getElementById(led).style.backgroundImage="url('Image/led_green.gif')";
}else{
document.getElementById(led).style.backgroundImage="url('Image/led_red.gif')";
}
}
</script>
and your html:
<div id="led1"></div>
<input name="" type="checkbox" id="ck1" onchange="ledonoff('led1','ch1')" value=""/>Led1
i think, thats it. i assume your image paths are correct.

Use this.
</script>
function ledonoff(led,chk)
{
if (document.getElementById(chk).checked == true){
document.getElementById(led).style.backgroundImage="url('Image/led_green.gif')";}
else {
document.getElementById(led).style.backgroundImage="url('Image/led_red.gif');}
}
</script>
<div id="led1"></div>
<input name="" type="checkbox" id="ck1" onchange="ledonoff('led1','ck1')" value=""/>Led 1

Related

If Checkbox is checked, remove attribute?

<div class="span1">
<input type="checkbox" value="l4" id="l4" field="" defaultValue="" appEditor="true"/>
</div>
<div class="span7">
<input type="text" class="m-wrap span10" id="fld_l4" defaultValue="" editType="intEdit" appEditor="true" disabled />
</div>
What I want to do is, If checkbox is checked, remove disabled in the fld_l4.
How to do this with using Prototype.js or jQuery?
EDIT: I'm using prototype with jQuery
i'm getting an error: Uncaught InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable. By the way i replaced $ with jQuery for conflicts
EDIT2 : Solved.
this.l4 = editor.instance;
editor.observe(iconstants.KEY_CHANGE,this.levelCheckboxChanged.bindAsEventListener(this))
Inside levelCheckboxChanged:
levelCheckboxChanged: function(e) {
if($("l4").checked == false) {
$("fld_l4").disabled = true;
} else {
$("fld_l4").disabled = false;
}
},
You can do it like this
$('#l4').change(function(){
if(this.checked){
$("#fld_l4").removeAttr('disabled');
}
})
Demo Link
This way should work:
$("#l4").on("change", function () {
$("#fld_l4").prop("disabled", !$(this).is(":checked"));
});
jsFiddle: http://jsfiddle.net/J5Nt2/1/
Here is the way to do this with PrototypeJS
inside of your DOM loaded event
document.observe('dom:loaded',function(){
$('l4').observe('click',function(){
$('fld_l4').writeAttribute('disabled',this.checked);
});
});
Plus you might want to change the id of '14' browsers do not like you starting id's with numbers - they let you do it but it is not part of the HTML spec
Try this,
if( $("#l4:checked").length ) {
$("#fld_l4").removeAttr("disabled");
}
Try this:
if( $("#l4").is(":checked") ) {
$('#fld_l4').removeAttr('disabled');
}

How to display message with the help of jquery?

i m trying to do a validation using jquery.
Enter Account no:
here is my html code
<input type="text" name="Assets" id="Assets" size="25" /><br />
<div id="eb"> Please Enter Valid Account no </div>
Enter Amount<input type="text" name="Liability" id="Liability" size="25" /><br />
and here is my jquery code
$(document).ready(function(){
$("#Assets").change(function(){
var Assets = $('input#Assets').val();
var expression=/[0-9]{4}\s[0-9]{4}\s[0-9]{2}\s[0-9]{10}/;
if (Assets == "" || Assets == " ") {
$('#eb').show();
}
else if(Assets.test(expression)){
$('#eb').hide();
}
$('#eb').toggle();
}
i want to display a mesage when user write a account no then at a time he can see the message that Please Enter Valid Account no and if he insert correct then that message can be hide.
here is my jsfiddle - http://jsfiddle.net/BzdfN/5/
please help
Try this code:
$(document).ready(function () {
$("#Assets").change(function () {
var Assets = this.value;
var expression = /[0-9]{4}\s[0-9]{4}\s[0-9]{2}\s[0-9]{10}/;
if (Assets.match(expression)) {
$('#eb').hide();
} else {
$('#eb').show();
}
});
});
This is a suggestion for more simple code. Notice I used match() which takes a regex as paramenter, if you want to use test() you should use the value/string as paramenter and use like expression.test(Assets).
Fiddle
About your code/fiddle:
you missed adding jQuery library to the fiddle
you missed ) closing the change() function
you missed }) closing the ready() function
you used test() with wrong order, check my text above about match() and test()
your fiddle with corrections: Fiddle
jQuery was not included and multiple syntax errors
$(document).ready(function () {
var expression = /[0-9]{4}\s[0-9]{4}\s[0-9]{2}\s[0-9]{10}/;
$("#Assets").change(function () {
$('#eb').toggle(!expression.test(this.value));
})
})//missing brackets
Demo: Fiddle
Try this - http://jsfiddle.net/dangoodspeed/BzdfN/9/ I made a bunch of changes, including activating jQuery, closing some functions, and reversed Assets and expression for the .test call
else if(expression.test(Assets)){

How to clear a text field?

I want to place a cross button next to a text field, which, on clicking it, clears the value entered by the user. In other words, it empties the field. Please help..
And I also want to focus the field, but after some 2 or 3 seconds..
Like this:
$('#myButton').click( function () {
$('#myField').val('');
});
Or without jQuery
document.getElementById('myButton').onclick = function () {
document.getElementById('myField').value = '';
});
Try this,
$('#button').click(function(){
$('#inputBox').val('');
});
Have you tried anything at all? But this should do (edit after misread, see below):
$('#your_button').click(function() { $('#your_textbox').val(''); });
In Javascript:
document.getElementById('textField1').value = "";
Well, learn to break your tasks into smaller one and everything will become much easier. Here, for example, you have 2 tasks:
1) Place a "X" button near input. This is achieved by CSS and HTML. You HTML might look like:
Then you should align your image with you input
2) Actual erasing. In jQuery:
$("#x_button").click( function() {
$("#input_id").val( "" );
});
But this is real basics of web development, so you should really consider to read some kind of book on it.
You can do it with html5 value.
<input type="text" placeholder="Your text here">
Assuming your text field looks like this one :
<input type="text" id="myText"></input>
and your button looks like this one :
<input type="button" id="myButton"></input>
You just have to do this in javascript :
<script type="text/javascript">
var myButton = document.getElementById('myButton');
myButton.addEventListener("click", function () {
document.getElementById('myText').value = '';
}, false);
</script>
If you're using jQuery it's even easier :
<script type="text/javascript">
$('#myButton').click(function() {
$('#myText').val('');
});
</script>
here is a sample:
Html:
<input type="text" id="txtText" value="test value" />
<input type="button" id="btnClear" value="Clear" />
javascript:
$(document).ready(function () {
$("#btnClear").click(ClearText);
});
function ClearText() {
$("#txtText").val("");
}

Checkbox to change font weight not working - Javascript

I need to insert a checkbox into a form to change the font weight of text in a div.
My javascript is as follows:
function boldText(checkBox,target) {
if(checkBox.checked){
document.getElementById("lineOne").style.fontWeight = "bold";
}
else {
document.getElementById("lineOne").style.fontWeight = "normal";
}
}
And my html like so:
<input type="checkbox" onclick="boldText(this,textToBold)">
<div id="lineOne">Change text to bold</div>
What is wrong here. I cant seem to get it to work.
remove target it is undefined so it gives an error send id of div in single quate as follow
<script>
function boldText(checkBox,target){ if(checkBox.checked){
document.getElementById(target).style.fontWeight = "bold"; }
else
{ document.getElementById(target).style.fontWeight = "normal"; } }
</script>
<input type="checkbox" onclick="boldText(this,'lineOne')">
<div id="lineOne">Change text to bold</div>
refer jsfiddle
Just delete target in function argument and textToBold when you call it
http://jsfiddle.net/xArCc/1/
or
http://jsfiddle.net/xArCc/2/
There you go. The target should've been "lineOne" when you make the call.
Change this line to :
<input type="checkbox" onclick="boldText(this,'textToBold')">
Because "textToBold" is a string.

Javascript Form Submit Failure

I have code on my website that isn't working, and I haven't been able to figure out why...
Here is the code:
if (self.location.href == top.location.href) {
document.fastform.submit();
document.getElementById(fastform).submit();
}
Now if I put something other than a form submit into the if statement, it works just fine. It's just when I do the form submit code it never works...
Here is the form code:
<form id="fastform" name="fastform" ACTION="/amember.php">
<INPUT TYPE="text" NAME="myurl" ID="myurl">
<input type="submit" />
</form>
Thanks for the help guys!
So far none of the suggestions work, I have tried several different variations like putting quotes around the fastform in getelementbyid. Here is my entire javascript program:
<script type="text/javascript">
function geturl() {
var locate = document.location
document.fastform.myurl.value = locate
}
window.onload = geturl;
if (self.location.href == top.location.href) {
var f=document.forms.fastform; f.submit();
}
</script>
Thanks for the suggestions!
Okay, so using some of the suggested code here I got it working. The problem was the if statement was not being executed at the right time, I moved things around so that the if statement was executed LAST and everything started working. Here is the complete (functioning) code:
<script type="text/javascript">
function geturl() {
var locate = document.location
document.fastform.myurl.value = locate
getmeoutofhere()
}
window.onload = geturl;
function getmeoutofhere() {
if (self.location.href == top.location.href) {
document.getElementById('fastform').submit();
}
}
</script>
<form id="fastform" name="fastform" ACTION="/amember.php" style="visibility:hidden;">
<INPUT TYPE="text" NAME="myurl" ID="myurl" />
<input type="submit" />
</form>
You can use this in your function:
var f=document.forms.fastform;
f.submit();
and it's working completely fine
document.getElementById('fastform').submit();
OR
var frm = document.getElementById('fastform');
frm.submit();
I'm not sure if it's the problem, but there's certainly one problem with the line:
document.getElementById(fastform).submit();
The problem, I think, is that you're trying to get an element by its id, but getElementById() requires a quoted string, unless you've already assigned the string to the variable represented by fastform. Therefore it should be either:
document.getElementById('fastform').submit();
or:
var fastform = 'fastform';
document.getElementById(fastform).submit();
Further, you seem to be trying to work with the fastform variable before it seems to have been set, in the first line contained within the if statement:
document.fastform.submit();
I'd suggest amending your script a little, to be something like:
if (self.location.href == top.location.href) {
var fastform = document.getElementById('fastform');
fastform.submit();
}
References:
document.getElementById().

Categories