Permalink
Browse files

set value method

  • Loading branch information...
1 parent 7a808ab commit 09209cc13152d9b213db8ae349b9e275c7b3777d @pascalopitz committed Dec 6, 2010
Showing with 42 additions and 22 deletions.
  1. +8 −1 index.html
  2. +33 −20 percentage.js
  3. +1 −1 percentage.min.js
View
@@ -40,8 +40,12 @@
<p class="percent">70%</p>
+ <p>
+ <input type="button" value="update above with 50%" id="update-p">
+ </p>
+
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
- <script type="text/javascript" src="./percentage.js"></script>
+ <script type="text/javascript" src="./percentage.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('input.percent').percentage();
@@ -61,6 +65,9 @@
$('#remove-span').click(function() {
$('span.percent').percentage('destroy');
});
+ $('#update-p').click(function() {
+ $('p.percent').percentage('set', 50);
+ });
$('p.percent').percentage({
color: 'pink',
View
@@ -1,7 +1,7 @@
(function(){
var _self = this;
- function render(conf) {
+ function render(conf, val) {
var elems = this;
if(conf == 'destroy') {
@@ -12,7 +12,7 @@
$(this.percentage).remove();
}
})
- return;
+ return elems;
}
var defaults = {
@@ -35,6 +35,34 @@
}
}
}
+
+ function getLeft(newVal) {
+ return Math.floor((conf.width * -1) + (conf.width / 100 * newVal))
+ }
+
+ function updateElem(elem, val) {
+ if($(elem).is('textarea,input,select')) {
+ $(elem)
+ .attr('value', val)
+ .val(val)
+ .attr('title', val + '%')
+ ;
+ } else {
+ $(elem)
+ .text(val + '%')
+ ;
+ }
+ }
+
+ if(conf == 'set') {
+ $(elems).each(function() {
+ var elem = this;
+ conf = elem.percentage_conf;
+ updateElem(elem, val);
+ $(elem.percentage).find('div').css('left', getLeft(val));
+ });
+ return elems;
+ }
$(elems).each(function() {
var elem = this;
@@ -52,28 +80,12 @@
hasValue = false;
}
-
- function getLeft(val) {
- return Math.floor((conf.width * -1) + (conf.width / 100 * val))
- }
-
function handleClick(e) {
var p = $(this).offset();
var percent = Math.ceil((e.clientX - p.left) / (conf.width / 100));
- if(hasValue) {
- $(elem)
- .attr('value', percent)
- .val(percent)
- .attr('title', percent + '%')
- .trigger('clickupdate')
- ;
- } else {
- $(elem)
- .text(percent + '%')
- .trigger('clickupdate')
- ;
- }
+ updateElem(elem, percent);
+ $(elem).trigger('clickupdate');
$(inner)
.css('left', getLeft(percent))
@@ -117,6 +129,7 @@
;
this.percentage = bar;
+ this.percentage_conf = conf;
});
return elems;
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 09209cc

Please sign in to comment.