1

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 () {

});
4
  • 4
    Why on earth do you need to do this? Just have two hidden input elements, crc_date and crc_choice. Commented Jan 11, 2012 at 13:40
  • 1
    and after your wrote the .click() method you stopped trying? Commented Jan 11, 2012 at 13:41
  • To select the element whose id="crc", you can use $('#crc') Commented Jan 11, 2012 at 13:42
  • I don't think, it is wrong question. I would rather upvote it..!! Commented Jan 11, 2012 at 13:46

10 Answers 10

2

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());
});
Sign up to request clarification or add additional context in comments.

2 Comments

Just one notice though: click will be fired only if the element is clicked (unless will be fired artificially by different script), so the better idea is to use change instead of click. As the name suggests, it will be fired when the value is changed, so OP's requirement ("item was selected by user") should be met then :)
Thanks for help. I knew only command "explode" for php, now I know how to use "split" in jQuery. Thank you.
1
$('.hidd').click(function () {
    var v = $('#crc').val().split('|');
    $('#crc').val(v[0] + '|' + $(this).val());
});

3 Comments

This will react only on clicks, so changes made in a different way (eg. by keyboard or using touch-enabled devices that may not fire click event) will not trigger the code OP needs.
True, but it looks like it is hidden anyway class="hidd" so there is no touch or keyboard access.
@PiTheNumber, the element holding the result is the hidden one, not the radio buttons that receive the click / change event..
1

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/

1 Comment

"The regex way" is nice, although it wouldn't work if he decided to put something other than numbers in the second part.
1

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());
});

Comments

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

Comments

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

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

Comments

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

Comments

0

Using regexp

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

Comments

0

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
});

Comments

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

3 Comments

This will append to the existing value, and could end up with 20120109|0|1|2|3
Rory, you are person with eagle's eyes :)
@RoryMcCrossan: At least it will be triggered at the correct moment, not only when clicked ;)

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.