OK I am just starting out with jQuery.
I have a page with a table of date-values. Each is wrapped in a tag which I can find with
$(‘mytag’).
<mytag>2009-10-31</mytag>
How, using Jquery, would I
- take each of the source values and
- pass it to a Javascript function
- then replace that source value within the table with the result of the function calculation.
So <mytag>2009-10-31</mytag>would be replaced with <mytag>Very Late</mytag>
I have the Javascript function written. My question is the jQuery syntax to pass the individual value.
Firstly, you will need an element selector, e.g.
Will select all <table> elements in your html. So,
will give you your elements. You will get a jQuery object (not a DOM object) returned. See http://docs.jquery.com/Selectors
Then you want to call a function for each of your elements. For this we call the .each function, and pass the function to call for each element:
(See http://docs.jquery.com/Utilities/jQuery.each)
The function in this case is called an Anonymous function
Then you want to reference the current object in the iteration, so we use the DOM
thisitem and wrap it into a jquery object. To get the value, we use the .text() function (http://docs.jquery.com/Attributes/text)Note: if it were an input element then you would have used .val()
Passing it to a function is easy:
The text() function has an overloaded implementation which allows you to set the text if you pass a value:
So, we can factor this into our code
Making our final code block: