2

I have implemented this function (libphonenumber javascript )in a website http://www.phoneformat.com/

How do i get the value returned by this html tag. Whether Yes or No

< DIV id="phone_valid" class="popup-value"></DIV>' 

I have tried this

function checkSubmit(){
var country=$("#phone_valid").val();
if(country=="No")
{
    alert("Not a valid number");
    return false;
}

So far no luck

4
  • 2
    For getting contents of a div use either innerHTML or textContent and not val. In jQuery the equivalents would be html() or text(). Commented Oct 18, 2013 at 13:40
  • @Harry: he's using jquery ! Commented Oct 18, 2013 at 13:41
  • What does .val() return ?, if it returns undefined maybe you need to use .html() or .text() Commented Oct 18, 2013 at 13:42
  • @SnakeEyes: Yup, late realization on my part, but already updated :) Commented Oct 18, 2013 at 13:42

7 Answers 7

3

The .val() method is primarily used to get the values of form elements such as input, select and textarea.

Use

$("#phone_valid").text();

to get DIV text content or

$("#phone_valid").html();

if you want markup.

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

Comments

1

First, no space between < and div (saw here: < DIV id="phone_valid" class="popup-value"></DIV>' )

Second:

function checkSubmit(){
var country=$("#phone_valid").text(); // it is a div not input to get val().
if(country=="No")
{
    alert("Not a valid number");
    return false;
}

Comments

1

You probably want to do this:

$("#phone_valid").html();

or

$("#phone_valid").text();

Comments

1

you can use this:

document.getElementById("phone_valid").innerHTML;

Comments

1

.val() is for form elements. You should use .text() or .html() to get the value from a DIV.

HTML

<DIV id="phone_valid" class="popup-value"></DIV>

JavaScript

function checkSubmit(){
    var country=$("#phone_valid").html();
    if(country=="No")
    {
        alert("Not a valid number");
        return false;
    }
}

Hope this helps!

Comments

0

In vanilla JavaScript you can use document.getElementById to get a specific node using ID:

var node = document.getElementById('phone_valid');

And then to get the text from that node you will need to use:

var text = node.innerText || node.textContent;

The jQuery .val() method is used on form fields like input, textarea, select...

Comments

0

If you need to include HTML comments then consider using contents() method

$('#mydiv').contents()

Other wise html() method or even text() will be what you are looking for because val() purpose is for form elements ;)

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.