0

I have this part of html on my form:

<label class="ideal-radiocheck-label" onclick="">
    <input id="pagamento_8" class="_input " type="checkbox" onclick="half(this,value);" checked="" value="980" name="pagamento[]" style="position: absolute; left: -9999px;" autocomplete="off">
    <span class="ideal-check checked"></span>
    988 (980-980 €)
</label>

It is a checkbox input button that call the function half() to make something like call the next function:

function setClickedLabel(clicked,new_value){
    label_e=$(clicked).parent();
    label_t=$(label_e).text();
    labels_t=label_t.split('-');
    $(label_e).html(labels_t[0]+'-'+new_value+' €)');          <-- 1
    //$(label_e).text(labels_t[0]+'-'+new_value+' €)');        <-- 2
    //$(label_e).innerHTML = labels_t[0]+'-'+new_value+' €)';  <-- 3
}

Here i would change a part of the label "988 (980-980 €)" (the last 980) and the "clicked" variable is the input object that you can see inside the label element.

Using one of the three methods reported i got 2 effect. Whit 1,2 it changes the label but it removes the input element. Whit 3 it doesn't make nothing.

What function i can use? How i must fix the code to get what i need? Html can't be changed.

Maybe another alternative without appending.

tnx, j.

5
  • What is half() ? Why you are calling it? i think you just misplaced it instead setClickedLabel() Commented Oct 28, 2013 at 13:00
  • value in that scope is probably undefined, but you're passing this, so just get clicked.value inside the function? Getting the textnode (< search for this) is somewhat more involved and requires some filtering, but should be straight forward. Commented Oct 28, 2013 at 13:01
  • 1
    jsfiddle.net/xBtWD Commented Oct 28, 2013 at 13:17
  • half() is a big function where among other things i call setClickedLabel(). Commented Oct 28, 2013 at 13:19
  • no value got the right scope because it's a constant and it's not refer to clicked.value. The problem is not on "undefined" value or the scope but i think on .text() .html() and innerHTML work. Commented Oct 28, 2013 at 13:22

1 Answer 1

0

Adapting the script on jsfindle left on the comment from adeneo i have wrote that solution. Maybe can be other better solutions.

function setClickedLabel(clicked,new_value,old_value){
    label = $(clicked).parent().contents().filter(function() {
        return this.nodeType === 3 && this.nodeValue.trim().length;
    }).get(0);

    label.nodeValue = label.nodeValue.replace(new RegExp("-"+old_value), "-"+new_value);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.