How do I get html tag value (div) in javascript

后端 未结 7 649
抹茶落季
抹茶落季 2020-12-21 11:01

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

相关标签:
7条回答
  • 2020-12-21 11:42

    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.

    0 讨论(0)
  • 2020-12-21 11:43

    you can use this:

    document.getElementById("phone_valid").innerHTML;
    
    0 讨论(0)
  • 2020-12-21 11:50

    You probably want to do this:

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

    or

    $("#phone_valid").text();
    
    0 讨论(0)
  • 2020-12-21 11:55

    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 ;)

    0 讨论(0)
  • 2020-12-21 11:59

    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;
    }
    
    0 讨论(0)
  • 2020-12-21 11:59

    .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!

    0 讨论(0)
提交回复
热议问题