Lucas
Lucas

Reputation: 3004

jQuery - How dynamically change the value?

I have in my html code:

<input class="hidd" type="radio" name="org1" value="1" />
<input class="hidd" type="radio" name="org1" value="2" />
<input class="hidd" type="radio" name="org1" value="3" />

<input type="hidden" id="crc" name="crc" value="20120109|0" />

I want to jQuery detect which item was selected by user and then set the value according to the formula: if radiobutton with value "1" is selected by user then input value of id="crc" should be: "20120109|1" instead of "20120109|0". So in other words - keep all numbers before sign "|" and change only value behind this sign "|". How can I do that? Maybe take value from id="crc", delete last number and add new number? Or maybe in another way?

$('.hidd').click(function () {

});

Upvotes: 1

Views: 2003

Answers (10)

Rory McCrossan
Rory McCrossan

Reputation: 337743

Try this:

$('.hidd').click(function () {
    var crc = $("#crc").val().split("|");
    $("#crc").val(crc[0] + "|" + $(this).val());
});

Or to cover all possible methods of selecting an option:

$('.hidd').change(function () {
    var crc = $("#crc").val().split("|");
    $("#crc").val(crc[0] + "|" + $(this).val());
});

Upvotes: 2

Tadeck
Tadeck

Reputation: 137460

Use jQuery's val() method, string's split() method and you will get something like:

var crc = $('#crc');
var radio = $(':radio[name="org1"]');
crc.val(crc.val().split('|')[0] + '|' + radio.val());

If you want this to be triggered when the value of the radio button is changed, then do the following (jQuery 1.7+):

$(':radio[name="org1"]').on('change', function(){
    var crc = $('#crc');
    crc.val(crc.val().split('|')[0] + '|' + $(this).val());
});

Upvotes: 1

Samuel Edwin Ward
Samuel Edwin Ward

Reputation: 6695

You may want to consider going beyond jQuery to the awesome power of JavaScript regular expressions: "20120109|0".match("[0-9]*\|") will return "20120109|".

Putting this together with your jQuery and requirements:

$('.hidd').change(function () {
    var crc = $("#crc"),
        old = crc.val(),
        num = $(this).val(),
        pfx = old.match("[0-9]*\\|"),
        nvl = pfx + num;
    crc.val(nvl);
    // here you will want to return true or false
});

Upvotes: 0

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196306

This should do it

$('.hidd').change(function () {
  var crc = $('#crc');
  var value = crc.val();
  crc.val( value.split('|')[0] + '|' + this.value );
});

Demo at http://jsfiddle.net/gaby/cmCS9/


or the regex way

$('.hidd').change(function () {
  var crc = $('#crc');
  var value = crc.val();
  crc.val( value.replace(/\d+$/, this.value) );
});

Demo at http://jsfiddle.net/gaby/cmCS9/1/

Upvotes: 1

Lautaro Orazi
Lautaro Orazi

Reputation: 166

Using regexp

   $('.hidd').click(function () {
        var crc = $("#crc"),
            $this = $(this);
        if($this.is(":checked")) {
            crc.val( crc.val().replace("/[0-9]$/", $this.val());
        }
    }

Upvotes: 0

Episodex
Episodex

Reputation: 4559

$('.hidd').click(function () {
    if($(this).is(":checked")) {
        $("#crc").val($("#crc").val().replace("/|[0-9]/","|" + $(this).val()));
    }
});

Upvotes: 0

Umesh Patil
Umesh Patil

Reputation: 10705

  $('.hidd').change(function () {
    var previousVal=$("input[type='hidden']").val();
    $("input[type='hidden']").val(previousVal+$(this).val());
});

Upvotes: -1

Toni Michel Caubet
Toni Michel Caubet

Reputation: 20183

$('.hidd').click(function () {

     $('#crc').val('20120109|' + $(this).val());
});

Upvotes: 0

PiTheNumber
PiTheNumber

Reputation: 23563

$('.hidd').click(function () {
    var v = $('#crc').val().split('|');
    $('#crc').val(v[0] + '|' + $(this).val());
});

Upvotes: 1

Steve
Steve

Reputation: 50583

$('.hidd').click(function () {
   $("#crc").val('20120109|' + $(this).val());
});

Upvotes: 0

Related Questions