Question

recently stakantin ( https://stackoverflow.com/users/1875610/stakantin ) helped me with a question that I have made about change the text of a label with this code:

$label=$this->add('View_HtmlElement')->setElement('h4')->set('Test'); $f=$this->add('Form'); $f->addField('Checkbox','click')->js('click',$label->js()->text('hallo world'));

I'm trying to do this (I cannot figure out how to do it):

I have a label "$label=$this->add('View_HtmlElement')->setElement('h4')->set('100');" and I have some fields of a form with check boxes. Each check box has a number.

Is it possible when the checkbox is clicked to do operations on that label ?

example:

Label: 100

field1 100 checkbox field2 200 checkbox

If I click checkbox of field1, then label value is 200 (its original value plus 100 of field1), and so on...

I don't know if I'm very clear buy thanks for any help. Alejandro

Was it helpful?

Solution

Example:

    $f = $this->add('Form');
    $l1  = $f->addField('Line','l1')->set(100);
    $cb1 = $f->addField('Checkbox','cb1','100');
    $l2  = $f->addField('Line','l2')->set(300);
    $cb2 = $f->addField('Checkbox','cb2','300');

    $cb1->js('change','
        if (this.checked) {
            $(this).parent().find("label")
                .text(parseInt(100) + parseInt('.$l1->js()->val().'));
        } else {
            $(this).parent().find("label").text(100);
        }
    ');

    $cb2->js('change','
        if (this.checked) {
            $(this).parent().find("label")
                .text(parseInt(300) + parseInt('.$l2->js()->val().'));
        } else {
            $(this).parent().find("label").text("300");
        }
    ');

But but this is not very good way to add javascript. Creating of js function based on my example in .js file is much clearer way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top