1

How can I get the value from the input field in the form. the idea is to send this value later to the server and display other values ​​associated with it.

<form action="" method="post" autocomplete="off" id="my_form"> <div class="txtb">{{form.date.label}} {{form.date}}</div> <div class="txtb"> <p class="txt"></p></div> <input type="submit" value="send" class="btn" id="btn"> 

In the Input field I assigned a class="pole" and trying to print value.

$('.pole').on('input', function() { var val = $('.pole').val(); $('.txt').html(val); $('.txt').val(val); }); 

but this does not work, how can this be done?

4
  • you want to display input value in p tag on input change ? Commented Feb 11, 2020 at 6:08
  • If you set value as html value of <p> it will not go to server as form values. You can use another input value in that case or better use input inside the form only? Commented Feb 11, 2020 at 6:09
  • @BInjal Patel yes. and try latter using ajax to transfer to the server and get an additional field with select Commented Feb 11, 2020 at 6:15
  • @ВадимШаройкин are you using angular ? Commented Feb 11, 2020 at 6:48

5 Answers 5

1

Change$('.pole').val() to $(this).val() You should have to try like

$('.pole').on('input', function() { var val = $(this).val(); $('.txt').html(val); $('.txt').val(val); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> <input type="text" class="pole" placeholder="type here"> <p class="txt"></p>

Sign up to request clarification or add additional context in comments.

3 Comments

whether this may not work due to the fact that the form is displayed from the backend model (django) ?
@ВадимШаройкин might be you can check it by inspecting .
I do not understand why this does not work in my case
1

You need to use $(this) to get the instance of current input with class=pole instead of $('.pole'), see below code

$('.pole').on('input', function() { var val = $(this).val(); $('.txt').text(val); }); 

2 Comments

value is not displayed in p tag
have you checked if val is showing current value properly? use console.log to print value in browser console
1

As you want to submit value, you need to mention input field in html. Have a look at this demo

<form id="myForm" action="/action_page.php"> First name: <input type="text" id="fname" value="Donald"><br> Last name: <input type="text" id="lname" value="Duck"><br> <input type="button" value="Submit" onclick="submitForm()"> <p id="demo"></p> </form> function submitForm() { var firstName = document.getElementById("fname").value; var lastName = document.getElementById("lname").value; document.getElementById("demo").innerHTML = lastName; } 

1 Comment

I want the value to be displayed not by pressing a button, but by input
1

I think your input is sibling of paragraph. Try this:

$('.pole').on('keyup', function() { $(this).parent().find(".txt").html($(this).val()); }); 

Comments

0

Not able to find dom with class txt & pole but you can try this code

$('.pole').on('input', function() { // changed here var val = $(this).val(); $('.txt').html(val); $('.txt').val(val); }); 

1 Comment

value is not displayed in p tag

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.