Jump to content

Basic problem learning jquery

- - - - -

  • Please log in to reply
3 replies to this topic

#1
onething

onething

    Programmer

  • Members
  • PipPipPipPip
  • 118 posts
I'm using the PHP JQuery Cookbook and very new to JQuery and code in general. Thing is, I can't figure out where to add the JQuery script in the following situation. The book tells me to setup a page called binding.html:

<html>

<head>

<title>Binding Elements</title>

<style type="text/css">

ul { background-color:#DCDCDC; list-style:none; margin:0pt;

padding:0pt; width:250px;}

li { cursor:pointer; margin:10px 0px;}

</style>

</head>

<body>

<ul>

<li>India</li>

<li>USA</li>

<li>UK</li>

<li>France</li>

</ul>

<select>

<option value="Africa">Africa</option>

<option value="Antarctica">Antarctica</option>

<option value="Asia">Asia</option>

<option value="Australia">Australia</option>

<option value="Europe">Europe</option>

<option value="North America">North America</option>

<option value="South America">South America</option>

</select>

<input type="button" value="Unbind select box"/>

</body>

</html>

and then add some JQuery:

Quote

It's time to add some jQuery magic. Attach a click event handler to list items using the .bind() method, which will set the background color of the clicked item to red. Attach the change event handler to the select box, which will display the value of the selected item. Finally, add a click handler to the button. Clicking on the button will remove the event handler from the select box.

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

$(document).ready(function ()

{

$('input:text').bind(

{

focus: function()

{

$(this).val('');

},

blur: function()

{

$(this).val('Enter some text');

}

});

$('li').bind('click', function()

{

$(this).css('background-color', 'red');

});

$('select').bind('change', function()

{

alert('You selected: '+ $(this).val());

});

$('input:button').bind('click', function()

{

$('select').unbind('change');

});

});

</script>

I'm not sure what it means by 'list items', but I've tried adding the script in different parts of binding.html but it remains the basic html document in the first quote.

I'm using xampp but even mentioning it doesn't make sense because JQuery is client side and xampp surely shouldnt have a say in whether script is recognized or not.

I added a JQuery library extension to the CS5 Dreamweaver I'm using, though it may have been a JQuery toolbar. I don't know what I installed actually and why it would have any consequence beyond CS5 showing JQuery suggestability while you write.

Could anyone be so kind as to tell me why the script not doing as it says it should in the book? Thanks.

#2
ferovac

ferovac

    Learning Programmer

  • Members
  • PipPipPip
  • 84 posts
You have to add the jQuery library to your page , you should use google help in this and just replace the line : "<script type="text/javascript" src="jquery.js"></script>" with "https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" from here

and then just c/p the javascript in head tag

#3
onething

onething

    Programmer

  • Members
  • PipPipPipPip
  • 118 posts

ferovac said:

You have to add the jQuery library to your page , you should use google help in this and just replace the line : "<script type="text/javascript" src="jquery.js"></script>" with "https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" from here

and then just c/p the javascript in head tag

Thanks.

However, it seems to be half working now. So I'd like to ask you two things, if I may.

It seems to be calling a css file at $(this).css('background-color', 'red'); but I'm not getting the red in the USA and France line that I see in the book. Should I set up a css file from scratch and call it, somehow, or is it that somewhere in the minified file from google red is really called by another word?

Shouldnt I be downloading the library in my xampp and calling it from there? Silly question really that has its pros and cons. I could work offline then, but I'd have to change all my file calls whenever I finished a template.

EDIT: Nvm, it works fine. It was the background color of each country when it's clicked upon. I clicked on them and they worked fine.

#4
ferovac

ferovac

    Learning Programmer

  • Members
  • PipPipPip
  • 84 posts

onething said:

Shouldnt I be downloading the library in my xampp and calling it from there? Silly question really that has its pros and cons. I could work offline then, but I'd have to change all my file calls whenever I finished a template.

Yes you can.

If your often offline while working you should download it and store on you server




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users