0

'tab_text' is a html code regenerated from 'mytable' by this code. All i want is for this regenerated html to have input values instead of input text fields. It will be used for some extraction task. For this , i found some code snippet and tried to edit that for my benefit:

var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
var j=0;
mytable = document.getElementById('countersTable'); // id of table

    for(j = 0 ; j < mytable.rows.length ; j++)
    {
        tab_text=tab_text+mytable.rows[j].innerHTML+"</tr>";
    }

    tab_text=tab_text+"</table>";

    tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, "") 

Obviously, I need to add a regex instead of "" at last line. I tried input[type=text][id^=*] It did not work.

Would you please help me find a solution to that issue?

2
  • input[type=text][id^=*] isn't a RegEx it's a jQuery selctor Commented Mar 20, 2015 at 14:57
  • 1
    @Liam or CSS selector Commented Mar 20, 2015 at 15:02

1 Answer 1

2

Don't use regular expressions here, it's much simpler to use DOM methods. In your case replaceChild will work well.

First of all, you need to select all input, fields. Then you iterate over the collection, and finally replace every with its value.

var inputs = document.querySelectorAll('input');

for (var i = 0; i < inputs.length; i++) {
    var text = document.createTextNode(inputs[i].value);
    inputs[i].parentNode.replaceChild(text, inputs[i]);
}
<div>
    Some <input type="text" value="Thomas" />
</div>
<p>One more <input type="text" value="Field" /></p>

In the demo above, all the input fields will be replaced. Of course, you can extend CSS selector in querySelectorAll to process other form elements if you need: input, textarea, select.

UPD. This is how you can combine above solution with your code to get text representation of the table with inputs turned into text:

var tab_text = "<table border='2px'><tr bgcolor='#87AFC6'>";
var j = 0;
var mytable = document.getElementById('countersTable'); // id of table

for (j = 0; j < mytable.rows.length; j++) {

    var rowClone = mytable.rows[j].cloneNode(true),
        inputs = rowClone.querySelectorAll('input');

    for (var i = 0; i < inputs.length; i++) {
        var text = document.createTextNode(inputs[i].value);
        inputs[i].parentNode.replaceChild(text, inputs[i]);
    }

    tab_text = tab_text + rowClone.innerHTML + "</tr>";
}

tab_text = tab_text + "</table>";

Demo: http://jsfiddle.net/hn7Lakx6/

Sign up to request clarification or add additional context in comments.

4 Comments

it must be done only inside 'mytable'. I mean, only inputs inside 'tab_text' must be converted to their values. How can i succeed that?
tab_text is id or class?
it is a html code regenerated from 'mytable' by this code. All i want is for this regenerated html to have input values instead of input text fields
Check this demo of how you can combine my solution with your code: jsfiddle.net/hn7Lakx6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.