Get XOOPS XOOPS FAQ Forums News Themes Modules
News World of XOOPS Developers Hacks Modules Themes Archive Submit News

XOOPS vs. Herko Coomans

Make a donation

Please select an amount to donate


Do you want your username revealed with your donation?
Yes - List me as a Generous Donor
No - List my donation as from an Anonymous Donor


Search

Local Support Sites

Cumulus Tag Cloud

admin Arabic banner block Christmas comments cumulus DayDawn dhsoft e-Commerce E-Learning Google GUI hacks instant-zero jQuery module news Nordic Olédrion oxygen PageRank security SEO simple-XOOPS sport tag Theme wiki xoops

New Users

Registering user

# 96568

dvsshoescom

Welcome to XOOPS!

Archives

XOOPS Code hosted on SourceForge

Tutorial Jquery: $.change, $.val and $.css

Posted by kaotik on 2009/11/10 8:01:21 (3444 reads) | Posted on Developer News
Learn how to detect changes on your forms using jquery and react accordingly using $.change


Jquery is great for detecting user actions and performing reactions based on these.

Let's take a simple form to start:

<form id="form1" name="form1" method="post" action="">
<
input type="text" name="mytext" id="mytext" />
<
input type="checkbox" name="mybox" id="mybox" />
</
form>


In this form we have a textfield and a checkbox.

STEP 1 - Check if checked

In our first test, when the checkbox is checked we want some text to appear in the textfield. There are 2 things we need for this:
1- wait for a change on checkbox "mybox". To acomplish this we will use a jquery function called $.change This detects any change on a particular selector.
2- If change does occur, place text inside textfield "mytext". For this we will combine a simple IF conditional with jquery function $.val used to set the value of a selector.

<script language="javascript" src="jquery-1.3.2.min.js"></script>

<script language="javascript">
$(document).ready(function() { //Finish loading the entire page before processing any javascript

$("#mybox").change(function () {
if ($('#mybox:checked').val() ) {
$("#mytext").val("yes");
} else {
$("#mytext").val("no");
}
});

});
</script>

<form id="form1" name="form1" method="post" action="">
<input type="text" name="mytext" id="mytext" />
<input type="checkbox" name="mybox" id="mybox" />
</form>


Now, as you click the checkbox, it will change the textfield value between "yes" and "no". Simple isn't it?

STEP 2 - Changing css through jquery

For this next step we will use our checkbox to change some css of our form.

<script language="javascript" src="jquery-1.3.2.min.js"></script>

<script language="javascript">
$(document).ready(function() { //Finish loading the entire page before processing any javascript

$("#mytext").css("border","medium solid green"); // initial color for my border: green

$("#mybox").change(function () {
if ($('#mybox:checked').val() ) {
$("#mytext").val("yes");
$("#mytext").css("border","medium solid blue"); //if checked apply blue
} else {
$("#mytext").css("border","medium solid red"); //if unchecked apply red
$("#mytext").val("no");
}
});

});
</script>

<form id="form1" name="form1" method="post" action="">
<input type="text" name="mytext" id="mytext" />
<input type="checkbox" name="mybox" id="mybox" />
</form>


You will see 3 new lines here. The first one:
$("#mytext").css("border","medium solid green"); // initial color for my border: green
defines our initial color for the border. I've applied a border to the form element "mytext" but I also could have applied it to any "div", "p", etc.
Note:
You can bind actions to selectores using their ID or class. For an id you would use: ("#mytext") , for a class you would use (".mytext")


Printer Friendly Page Send this Story to a Friend Create a PDF from the article


Bookmark this article at these sites

                   

The comments are owned by the poster. We aren't responsible for their content.

Nice one! Maybe you should add some explanation on how to use the above, when the ids are generated dynamically. That's what drove me crazy a while ago.

I would love to see your lessons continued.
Posted: 2009/11/10 11:54 • Updated: 2009/11/10 11:54
I would love to see someone (maybe myself) do a JSON tutorial, I think it should be used as much as possible with select boxes so on..

Anyway time to go to work..
Posted: 2009/11/10 16:26 • Updated: 2009/11/10 16:26
Hi frankblack
Glad you like it. I'm writing a new one that should cover what you ask.

Hi wishcraft
I'll put that on my wishlist.

NOTE:
In this tutorial, i didn't make one thing clear; $.val is used to retrieve the value of ONLY form elements. To retrieve text from a "div" you would use $.text or if you also wanted to retrieve all html code inside that "div" you could you $.html. When using $.text, it will strip all html code from it.
Posted: 2009/11/11 5:21 • Updated: 2009/11/11 5:21
I was looking for a way to send tokens along with the form using jquery, but the normal xoopstoken does only work once and mimicing the createtoken-function in ajax was too much work for me. But I stumbled upon an excellent article sending tokens with jquery. README
Posted: 2009/11/26 7:31 • Updated: 2009/11/26 7:31
Interesting tutorial.
Are you going to use this method by itself? Or will you combine it with a captcha to have 2 levels of safety on your forms?
Posted: 2009/11/26 11:22 • Updated: 2009/11/26 11:22
Yes, I will use it for myself for some parts where a captcha is not needed, but at one place I want to use recaptcha.
Posted: 2009/11/26 11:26 • Updated: 2009/11/26 11:26
Found some other captchas. But the first one is not really reliable, but looks nicer.

Captcha 1

Captcha 2

I managed to get captcha 2 to work, but I don't know how to restart this one. But I think this not really needed, because he verified before and at one point the token expires anyway, so tell-a-friend will not get abused (so often I hope).
Posted: 2009/11/27 12:31 • Updated: 2009/11/27 12:31
I found something useful on the internet. As above examples show, the inline jquery part can be quite big. To move the jquery part to external files and to avoid the redundancy of document.ready there is a simple strategy. The example is taken from upcoming debaser 3.09. Some things are not useful to you, but I just wanted to show the way.

To avoid the redundancy of document.ready I wrote the first lines to be:
$jstohead '$(document).ready(function() {';

Ok, here we start with document.ready. The next lines are:
if ($xoopsModuleConfig['useffmpeg'] == 1) {
$xoTheme->addScript(DEBASER_UJS.'/hovermovie.js', array('type' => 'text/javascript''charset' => _CHARSET), null);
$jstohead .= 'hovermovie();';
}

I just added a path to the external file and then added the function call used in the external js.
Next lines would be:
$jstohead .= '});';
$xoTheme->addScript(null, array('type' => 'text/javascript''charset' => _CHARSET), $jstohead);

And how is the external file looking? Here we go:
(function($){
hovermovie = function() {    
$(
".imgHoverable").hover( function() {
var 
hoverImg HoverImgOf($(this).attr("src"));
$(
this).attr("src"hoverImg);
}, function() {
var 
normalImg NormalImgOf($(this).attr("src"));
$(
this).attr("src"normalImg);
}
);
function 
HoverImgOf(filename) {
var 
re = new RegExp("(.+)\\.(gif)""g");
return 
filename.replace(re"$1_hover.$2");
}
function 
NormalImgOf(filename) {
var 
re = new RegExp("(.+)_hover\\.(gif)""g");
return 
filename.replace(re"$1.$2");
}
};
})(
jQuery);


So we tidied up the file holding the former jquery part. I think is extremely useful, when reusing jquery functions in more than one file.

Have fun!

edit: oops, forgot to mention the source click
Posted: 2010/1/4 13:34 • Updated: 2010/1/4 13:36
Forgot to mention that "of course" you can pass parameters to the function.
$jstohead .= 'hovermovie(avartopass);';

hovermovie = function(avartoretrieve) {

Posted: 2010/1/5 6:06 • Updated: 2010/1/5 6:06