简体   繁体   中英

how can i list in html names in textarea

How can i catch id tags and write into p. I want to list id names in to textarea inputs.

Thanks.

HTML

<textarea id="textid" class="textclass">
    <div id="hello"><span id="world"></span></div>
</textarea>
  <p></p>

JS

$("textarea").keyup(function () {
      var value = $(this).val();
      $("p").text(value);
 }).keyup();

Sample http://jsfiddle.net/6YRyP/2/

DEMO

$("textarea").keyup(function () {
    var $content = $($('<div/>').html($.trim(this.value))),
        ids = "";
    $content.find('[id]').each(function(){
        ids += " "+ this.id;
    });

    $("p").text(ids.substring(0));
}).keyup();

If you don't want to parse the text from textarea into HTML (as @roasted proposes) you can try using regular expressions:

$("textarea").keyup(function () {
     var value = $(this).val();
     val = '';

     matches = value.match(/id="[^"]*/g);

     for (i in matches) {
         val += matches[i].substr(4) + ' ';
     }

     $("p").text(val);
}).keyup();

This one way to do it : http://jsfiddle.net/6YRyP/7/

$("textarea").keyup(function () {
    var value = $(this).val();
    $('body').append('<div id="search">'+value+'</div>');
    var listOfId = "";
    $('*', $('#search')).each(function() {
        var currentId = $(this).attr('id');
        if (currentId != "") {
           listOfId = listOfId + currentId + ' ';
        }
    });
    $('#search').remove();
    $("p").text(listOfId);
}).keyup();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM