0

Is there some way of determining at run time, if an element returns value for element.text() or not? Using javascript or jquery?

I.E. some way of checking if an element is pure text, or it is some other type of element?

Why I need a solution to the above---

I am trying to parse through some complicated forms with totally different coding styles (in the way, for example, text values for elements of a radio button may be enclosed in label tags, or they may be directly given, or they may be enclosed in span tags, and so on...)

So I need to parse the form with the form id as wrapper,

now if the text value for a radio button is enclosed in span, and current selected element is radio button, then next element will be the span tag (opening) which I want to do nothing with and move on. The one after that will be the text, and this I want to obtain using this.text().

Hence the whole question...

3 Answers 3

1

You can use nodeType to check if an element is pure text (in which case its value will be 3)

<div id="wrapper"><input type='radio' />some text here</div>

$('#wrapper').contents().each(function()
                   {
                        alert(this.nodeType);
                   });

It will alert 1 (for input) and 3 (for text). For type=3, you can use text() to get text value

Note- It'll also taken into account white spaces (as text nodes).

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

3 Comments

I know it's picky, well it's not at all really with javascript, but make sure the curly braces don't start on a newline when using javascript, bad syntax otherwise.
@Vikk - I am trying to parse through "<input type="radio" name="lunch" value="pasta1" /><span>Pasta</span>" but the method suggested by you (obtain nodeType) is not able to pick up the text "Pasta" after ignoring the opening "<span>" element...Is this a limitation of the text() command itself? I just want to skip div/span/label/ other such element types if the specified element type is enclosing the text ...(Pasta in my example)... Or do I have to take a different approach for label/span/div respectively?? Thanks...
Since Pasta is enclosed in span tags, it is no more a text node. It's a span tag now. Please update what you're trying to do here - jsfiddle.net/SrKxP
1
var attr = $(this).attr('name');

// For some browsers, `attr` is undefined; for others,
// `attr` is false.  Check for both.
if (typeof attr !== 'undefined' && attr !== false) {
    // ...
}

Another way to check:

if( typeof( $(this).attr('name') ) != 'undefined' ) { // ... }

you can create you own extension method of jQuery:

$.fn.hasAttr = function(name) {  
   return this.attr(name) !== undefined;
};
$(document).ready(function() {
    if($('.edit').hasAttr('id')) {
        alert('true');
    } else {
        alert('false');
    }
});



 <div class="edit" id="div_1">Test field</div>

Comments

0

hi you can check the type of element as follow

   <form >
    <input type="text" id="a" value="abc"/>
    </form>
    <script>

       var type = document.forms[0].elements[0].type;
       alert(type);

    </script>

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.