Aras Serdaroğlu
Aras Serdaroğlu

Reputation: 321

How to get and print input type number with jQuery?

I want to print an input type number on confirmation page.

var displayConfirm = function() {
            $('#tab5 .form-control-static', form).each(function(){
                var input = $('[name="'+$(this).attr("data-display")+'"]', form);
                if (input.is(":radio")) {
                    input = $('[name="'+$(this).attr("data-display")+'"]:checked', form);
                }
                if (input.is(":text") || input.is("textarea")) {
                    $(this).html(input.val());
                } else if (input.is("select")) {
                    $(this).html(input.find('option:selected').text());
                } else if (input.is(":radio") && input.is(":checked")) {
                    $(this).html(input.attr("data-title"));
                } 
            });
        }

If I use input.is(":number") and $(this).html(input.val()); it crashes.

Upvotes: 1

Views: 3449

Answers (3)

George
George

Reputation: 36784

For completeness, you can register a jQuery pseudo selector if you wanted to stick with :number:

$.expr[':'].number = function(el){
  var $el = $(el);
  return $el.prop('tagName').toLowerCase() == 'input' && $el.prop('type') == 'number';
}

.is(':number') would then work perfectly fine for you.

JSFiddle

Upvotes: 1

guradio
guradio

Reputation: 15555

$('#qwe').keyup(function () {
    alert($(this).val());

});

FIDDLE

To get value of input element use .val()

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82231

The reason it crashed because :number is not valid selector. You need to use attribute equals selector in .is() condition:

input.is("[type=number]")

Snippet:

else if (input.is("[type=number]")) {
    $(this).html(input.val());
} 

Upvotes: 1

Related Questions