1

I am able to populate paragraph using script below, but how can I populate input field value using this script? Any idea?

$(function check(){     
     $(document).ready(function() {
        $('#forma').submit(function(e){
        e.preventDefault();    
        $('.qty').each(function(){ 
           if($(this).val() != '0')
             {
                $('#output').text($('#output').text()+ ' ' + $(this).attr('name') + ' ' + $(this).val()+ ';' );
             }
          });
        });
     });
});
4
  • $('#someInput').val('some value'); ? Commented Oct 2, 2014 at 16:40
  • This script checks all input fields inside form and gets its value and name if value is greater than 0 and shows result in paragraph with output id. I have tried to add same id to the input field, but nothing happens. I am very beginner in javascript so can you explain your answer little bit better. Thank you. Commented Oct 2, 2014 at 16:47
  • Well, if all you tried was to change the selector then that won't work. You can't set the .text() of an input because it doesn't have a "text" to set. It has a "value". It's a subtle semantic difference, but an important one. So to set the value you would use the .val() function. Commented Oct 2, 2014 at 16:50
  • Just tried as soon as I saw @fritzi2000 answer and it works. Thank you both of you for great help. Thank you again. Commented Oct 2, 2014 at 16:54

1 Answer 1

1

What you have to do is essentially the same as with the paragraph, i.e. you select the element by its id or class. The difference is that for input fields you have to use .val("someValue"), whereas for paragraph you have to use .text("SomeText") just as you did.

Hope this helps.

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.