1

I have to convert table to csv I am using following logic to concatenate strings of each td

 var $text=(table.rows[0].cells[i].innerHTML.split(", ").join("")).toString();

Assume $ text actually holds the <font class="Major">testcasedff</font>;

When I tried to use Find function for extracting data

var data=$text.find('font').text();

It actually doesnt work .I am using IE9 as test browser and im getting the following script error

Line: 300 Error: Object doesn't support property or method 'find'

Please refer to the Fiddle below

6
  • $text is s string... You are then trying to use find on it Commented Sep 4, 2014 at 9:24
  • 3
    you need to use $($text).find(...) Commented Sep 4, 2014 at 9:25
  • $text holds <font class="ClassName">Test Subject1 </font> It is a string Commented Sep 4, 2014 at 9:25
  • You are using the wrong character to delimit your code for display in this question... Please refer to my edits :) Commented Sep 4, 2014 at 9:25
  • var data=$($text).find('font').text(); It doesnt give any result . Commented Sep 4, 2014 at 9:34

2 Answers 2

1

Try:

In the following row JQuery convert an HTML Element from string to JQuery Object and so your op will be simplified:

var fontObj = $("<font class='Major'>this is a test</font>");
var $textfontObj.text();

In pure javascript You will need to create a dummy element, insert yout HTML Element from string and then get the desired result:

var targetEleContainer = document.createElement('div');
targetEleContainer.innerHTML = "<font class='Major'>this is a test</font>";
var out2=(targetEleContainer.firstChild).innerHTML;

bye

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

2 Comments

Sorry @AbinayaDevarajan, it works only if you use JQuery. Instead if you use only javascript without JQuery you are right, it does not work! I supposed you are interested in a simple and rapid way to transform a string to JQuery object and then get the property.
Hi @AbinayaDevarajan try again your demo: i changed it and added my code: check for "This is " on jsfiddle.net/8zrhgy6v/6 Let me know
0

textContent dom Property and Parse html works

  var $text="<font class='Major'>this is a test</font>";

  var result='';
   var data=$.parseHTML($text);
            $.each(data, function (i, el) {
            result += el.textContent;
            });

 alert(result);

Please Checkout the Fiddle

http://jsfiddle.net/Abinaya_WEBGurl/8zrhgy6v/3/

Comments

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.