Capture field value by its ID and turn into a variable

Asked

Viewed 2,955 times

1

I want to take the value of a field from my table and turn it into a variable.

Example:

<td id='exemplo'>VALOR EXEMPLO</td>

want to take the content and turn it into a variable using javascript, I believe q is easy but n understand a lot of programming

I tried it but I couldn’t:

<script type="text/javascript">
var teste = $("#exemplo").val()
</script>

and to call her in php:

<?php

$variavelphp = "<script>document.write(teste)</script>";

echo "$variavelphp"
?>

2 answers

1


Change your line of code js to the following:

var teste = $("#exemplo").html();

Would look like this:

var teste = $("#exemplo").html();
alert(teste);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table>
  <tr>
    <td id='exemplo'>VALOR EXEMPLO 1</td>
    <td id='exemplo1'>VALOR 2</td>
  </tr>
</table>

First, the call of the event of the JQuery $("#exemplo").val() refers to the attribute value of some element, where the values of the elements are usually stored, but their element in question is actually a td(structure of the body of a table) which, besides everything, also does not have an attribute value native, to take its value, the certain semantically would be to put another element inside it, for example a span or a label, that would look something like this: <td><label id='exemplo' value='VALOR EXEMPLO 1'>VALOR EXEMPLO 1</label></td>, so your command line JQuery nor would need to be changed. But this will go according to each...

Structuring the way you want, the element td just "write" on the screen what is contained in it, ie the type of element td does not have the attribute natively value. To take the content/value contained within a td, you should use the event JavaScript innerHTML(); that in JQuery is equivalent to the event HTML();

  • worked perfectly for what I wanted, thank you very much

0

If you want to take the content of an element in JS, just this:

var teste = document.getElementById('exemplo').innerText;

But it’s worth saying that it won’t work with a td loose, without the table structure.

See a functional example:

var teste = document.getElementById('exemplo').innerText;

//So para demonstrar:
document.body.innerHTML += 'O valor é ' + teste;
<div id="exemplo">VALOR EXEMPLO</div>

In this case I used a div to avoid the problem mentioned with the td loose.

Was used the innerText, which is preferable to take the textual content. If you want to take something that contains child elements, give preference to innerHTML.


If your doubt is about PHP variables, then the subject changes, and it’s worth a read on:

Match php variable to javascript variable

  • I believe what he said was wrong, it becomes clear when he sees the last block of code. Thus, his answer is not correct, because he himself solved the problem of taking the value of an element and putting it in a variable, only in Javascript.

  • thanks for the help, I read the question about matching varial php to one of javascript and helped me by having to do this.

  • @user5978 I answered the part that was not duplicate, because it was clear that the author does not have much experience with this part of integration. The PHP part I put the link to answer that has what it looks for, because it will not help anyone I put repeated content on the site. If you look at the editing history, you will see that I actually had closed as a duplicate, but I preferred to cover all possible conditions to help the author, so I reopened and answered with what was missing: http://answall.com/posts/125296/revisions

  • @user5978 now, if you point out where my answer is wrong, then we can see what to do. I reread everything, and it didn’t make much sense that your comment. If you could be more specific about which part is not correct, it would help. After all, I showed how to do in JS (which he will need anyway, even passing to PHP), and pointed to how to do in PHP. Missed what?

  • The guy says he needs to take a Javascript variable, and he should pass it to PHP. And he states that he tried in a certain way (shows that he correctly picked up the value with Javascript), but that he was unable to get this value to PHP.

  • Showing another way to do what he’s already done, without even hinting at what he really needs, is not a solution to the problem. In my humble opinion.

  • @user5978 So your suggestion is instead of putting the link, play the other answer here? That I won’t do. What I realized is that the other answer that does the same as mine, but it needs jQuery, you haven’t said anything, so that clears up other things as well. Anyway, thanks for the return.

  • I usually use comments to point out external solutions, when I have nothing to add (Regarding the content of the link).

  • True, I hit my eye on that answer, and I could swear that the guy had suggested using ajax ... I was going to comment on it ... but enough bullshit for today :D

  • @user5978 is no bullshit, rest assured that for me it is a good one. You pointed something out, and I wanted to understand better. That’s all. I respect your opinion 100% regardless of whether you agree or not, and I think it’s very good that you express it. And feel free to criticize whenever you feel it necessary.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.