2

Given the following:

<p>This is a <fund id="12345">Test Fund</fund>. More text.</p>

How do I extract the text ("Test Fund") from the fund element?

I have tried the following:

$('fund').each(function(index) {

    alert($(this).text());

});

I can retrieve the id attribute as follows:

alert($(this).attr("id"));

I have had no luck with text() html() etc.

EDIT: Initially I had only tried IE8, But after test in firefox I discover it works with FF just fine. I then reviewed in IE dev tools and noticed that the Dom explorer was treating the opening tag as a stand-alone node, as well as doing this to the remaining text, and closing tag.

I found some info on using a custom namespace as follows:

<html xmlns:myns>
<head>
<title>Test</title>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.4.min.js"></script>

<script type="text/javascript">
    $(document).ready(function(){
       // Your code here
       $('myns:fund').each(function(index) {
            alert(index + ': ' + $(this).attr("id"));
        });
     });
</script>

</head>
<body>
<p>Sample paragraph.</p>
<p>This is a <myns:fund id="12345">Test Fund</fund>. More text.</p>
</body>
</html>

This fails also but renders correctly in the IE DOM explorer.

2
  • What kind of document are you working in? HTML? Commented Dec 17, 2010 at 14:18
  • You should update this to correct the } error, and note the IE8 issue. Commented Dec 17, 2010 at 16:21

2 Answers 2

2

Your code works fine for me in Firefox 3.6 when I add the missing curly brace before the last parenthesis:

$('fund').each(function(index) {
    alert($(this).text());
});

The above correctly prints Test Fund.

EDIT: I got it working in Internet Explorer 8 by calling html() on the <p> element, applying $() to the result and calling text() on that:

$(document).ready(function(index) {
    alert($($("p").html()).text());
});
Sign up to request clarification or add additional context in comments.

5 Comments

Since posting the question I did test in firefox and discovered in works fine (without the typo!). After opening the IE8 developer tools I noticed that The opening tag is being treated as a stand alone node and everything else as child nodes of the "<p>" element. Fixing IE is the goal. Any thoughts?
@HectorMac, same here. I'll try to find something.
It's really discouraging to look at the selector library's code and see that it selects by using the native getElementsByTagName which DOES work in IE8, but $('element') won't in this case. This may be a bug and may be noted in the sizzleJS bug page.
Yeah but that works for the wrong reasons... <p>This is a<blah>now what?<level2>???</level2></blah> <fund id="12345">Test Fund</fund>. More text.</p> There's something awkard with the sizzleJS library and IE8.
@user257493, I agree that's not a generic solution. I only tried to coerce good old IE into giving the proper result from the questioner's example :)
0

Try grabbing the contents of the <p> tag as an HTML string, and passing that to the jQuery constructor, rather than trying to get the fund tag direct from the page:

var html = $("p").html();
alert($("<div>" + html + "</div>").find("fund").text());

Edit: you need to wrap the html inside a div (or other) element so jQuery doesn't get confused and think it's a selector. Works here: http://jsfiddle.net/uRggd/1/

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.