RSS

Parse html and get / set attribute values

19 Apr

Parse the html using javascript and get the attribute values.

<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function return_MemOfIdByTag(a,b){
 return jQuery(b,jQuery(a));
}
function parseHtml(){
 var htmlString = document.getElementById('parseTest').innerHTML;
 var editorObj = document.createElement('div');
 editorObj.innerHTML = htmlString;
 var input_tags=return_MemOfIdByTag(editorObj,'input');
 if(input_tags.length > 0){
    for(var index = 0; index < input_tags.length; index++){
      var type = input_tags[index].getAttribute('type');
      var name = input_tags[index].getAttribute('name');
      var id = input_tags[index].getAttribute('id');
      document.getElementById(id).setAttribute('value','testValue');
      document.getElementById(id).setAttribute('table','tableValue');
     document.getElementById(id).setAttribute('column','columnValue');
   }
 }

 var select_tags=return_MemOfIdByTag(editorObj,'select');
 if(select_tags.length > 0){
   for(var index = 0; index < select_tags.length; index++){
     var type = select_tags[index].getAttribute('type');
     var name = select_tags[index].getAttribute('name');
     var id = select_tags[index].getAttribute('id');
     document.getElementById(id).setAttribute('value','testValue');
     document.getElementById(id).setAttribute('table','tableValue');
     document.getElementById(id).setAttribute('column','columnValue');
   }
 }
 var ta_tags=return_MemOfIdByTag(editorObj,'textarea');
 if(ta_tags.length > 0){
   for(var index = 0; index < ta_tags.length; index++){
     var type = ta_tags[index].getAttribute('type');
     var name = ta_tags[index].getAttribute('name');
     var id = ta_tags[index].getAttribute('id');
     document.getElementById(id).setAttribute('value','testValue');
     document.getElementById(id).setAttribute('table','tableValue');
     document.getElementById(id).setAttribute('column','columnValue');
   }
 }
}
</script>
</head>
<body>
<div id="parseTest">
 <input type='text' id='name' name='name'>
 <input type='text' id='id' name='id'>
 <input type='text' id='last' name='last'>
 <textarea id='address' name='address' cols='20' rows='3'></textarea>
</div>
<input type="button" onclick="parseHtml()" value="click"/>
</body>
</html>
 
Leave a comment

Posted by on April 19, 2013 in Javascript, jquery, Strings

 

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: