User1899289003 User1899289003 - 1 year ago 98
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.

Answer Source

Change :

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

To :

var p = $('#p1').text();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download