User1899289003 User1899289003 - 8 months ago 41
Javascript Question

How to get value from a dynamic element

I need to get the value from a "p" element, I draw this "p" with jQuery and it's ok, then I have a button and when I click on it I want to display the value from "p" element but I don't get any information, here is a simple code example:

$(document).ready(function() {
$('#c').click(function() {
var p = $('#p1').val();


function draw() {
var html = "";
html += '<p id="p1">Hi</p>';



<script src=""></script>

<button id="c">Click</button>
<hr />
<div id="d">

How can I solve this? I don't get any console error.


Change :

var p = $('#p1').val();

To :

var p = $('#p1').text();