Richa
Richa

Reputation: 3289

Use ID from one page in JQuery

I have dynamic HTML in my page. Now i want to use Id and data from one Js file to Other.

I have following HTML in care.js file

$('#chat-box').html(
   '<p style="margin-top:10px;">Enter Your Name</p><input type="text" id="chat-box-name"  class="chat-editing" />'
                   );

Now i want to use the data entered in above input box in chat.js.

Is it possible that i can usethe id/data from one JS file to other in same project??

Thanks

Upvotes: 0

Views: 44

Answers (1)

Mark
Mark

Reputation: 3272

When this code executes you will have a element with your code as child:

<div id="chat-box">
  <p style="margin-top:10px;">Enter Your Name</p>
  <input type="text" id="chat-box-name"  class="chat-editing" />
</div>

If you make sure the code from the second JavaScript file only gets executed after you added the content to #chat-box you can use JavaScript to get the value of the inputbox. Assuming that you use jQuery, like you did in your question:

var input = $('#chat-box-name').val(); //The input

Pure JavaScript:

var input = document.getElementById('chat-box-name').value;

Upvotes: 2

Related Questions