Get value from div.

Is there a way to get the value from the div element in the form instead of using input?

I know this is somehow a dumb question, but I just want to know if this is possible. Something like a PHP DOM?

Or is there a way to change the div to the input when submit is clicked?

Thanks,

+4
source share
6 answers

With Javascript, you can put the contents of a div in a (hidden) input right before submitting the form.

In jQuery it would look something like this:

$('#myForm').submit(function() { $('#myHiddenInput').val($('#myDiv').html()); }); 

Edit: Why are you guys talking about AJAX? In this situation, there is no need to use AJAX!

+5
source

Here's a javascript-free jQuery solution:

 <script> function prepareSubmit() { var payload = document.getElementById("target").innerHTML; document.getElementById("divContents").value = payload; return true; }​ </script> 

Markup:

 <form name="test"> <div id="target">Payload</div> <input type="hidden" id="divContents" name="divContents" /> <input type="submit" onclick="return prepareSubmit()" value="submit" /> </form> 

The onclick function is run before the send event. In fact, the form will not be submitted if prepareSubmit does not return true. It inserts the contents of a div into hidden input. I think this is the easiest solution. Make sure the script is loaded before the form.

+3
source

PHP code is processed on the server side, so basically there can be no PHP manipulation of the DOM on the client side.

Using JavaScript (the easiest imho with jQuery), you can pull information from the DOM and include it in the presentation form as additional fields.

There are tons of questions in SO related to adding a form to a view using jQuery, for example.

Safari / JQUery - insert and update a hidden field on a page

+2
source

Use jQuery to put information in a div. Very simple to do, just a few lines of code.

0
source

you can use ajax call in javascript file. All you have to do is call div id, for example, like $ ("# yourdiv"). Val (); This will give you the value of what is in the id div. If you give a more specific example and show part of your code, I can be more specific. Jim

0
source

There are several ways to archive this.

Hidden fields

You can create hidden input fields and fill them with javascript when sending

Ajax

Use, for example, the jQuery AJAX library to serialize data and send it through AJAX

Style input fields, so they look like the div you are talking about

Another option is to simply use the input fields during use and style them so that they no longer look like inputs.

Let me know if you need an example of any of these.

0
source

Source: https://habr.com/ru/post/1415645/


All Articles