Phoenix.1993
Phoenix.1993

Reputation: 85

Search for strings in a textarea which is contained inside a DIV

<div id="right_pane">
<textarea id="TextArea1"></textarea>
<input id="Submit1" type="submit" value="submit" />
</div>

Hi, I have a text area and a submit button inside a DIV element. The input to the text area is going to be the source code of a website. I need to search and find number of Text box elements contained in the source code and display it to the user when the Submit button is clicked. Can someone help me with the code?

Thank You in advance.

Upvotes: 1

Views: 639

Answers (1)

KiiroSora09
KiiroSora09

Reputation: 2287

You can try the below. Since you did not provide a sample source code input, I only tested this with the below sample source code:

Sample Source Code

<div>
<input id="textbox1" type="text"/>
</div>
<input type="text" id="textbox2"/>

Demo

var
$textarea = $('#TextArea1'),
$submit = $('#Submit1');

// Apply test input
$textarea.val('<div><input id="textbox1" type="text"/></div><input type="text" id="textbox2"/>');


$submit.click(function(e) {
  e.preventDefault();

  sourceCode = $textarea.val();

  // Create jQuery object to insert and search the source code (from the textarea)
  var $searchObject = $('<div id="searchThis"></div>');

  // Append the source code (converted to a jQuery object)
  $searchObject.append($(sourceCode));

  // Search the object for occurrence of type="text" inputs
  alert($searchObject.find('[type=text]').length);
});
textarea {
  width: 400px;
  height: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="right_pane">
  <textarea id="TextArea1"></textarea>
  <input id="Submit1" type="submit" value="submit" />
</div>

Upvotes: 1

Related Questions