Get value of div content using jquery

后端 未结 5 1721
轮回少年
轮回少年 2020-11-28 11:09

I have the following html and I want to get the value of the div which is \"Other\" How can I do this with jQuery?

 
相关标签:
5条回答
  • 2020-11-28 11:36

    your div looks like this:

    <div class="readonly_label" id="field-function_purpose">Other</div>
    

    With jquery you can easily get inner content:

    Use .html() : HTML contents of the first element in the set of matched elements or set the HTML contents of every matched element.

    var text = $('#field-function_purpose').html(); 
    

    Read more about jquery .html()

    or

    Use .text() : Get the combined text contents of each element in the set of matched elements, including their descendants, or set the text contents of the matched elements.

    var text = $('#field-function_purpose').text();
    

    Read more about jquery .text()

    0 讨论(0)
  • 2020-11-28 11:36

    $('#field-function_purpose') will get you the element with ID field-function_purpose, which is your div element. text() returns you the content of the div.

    var x = $('#field-function_purpose').text();
    
    0 讨论(0)
  • 2020-11-28 11:40

    Try this to get value of div content using jquery.

        $(".showplaintext").click(function(){
            alert($(".plain").text());
        });
        
        // Show text content of formatted paragraph
        $(".showformattedtext").click(function(){
            alert($(".formatted").text());
        });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <p class="plain">Exploring the zoo, we saw every kangaroo jump and quite a few carried babies. </p>
        <p class="formatted">Exploring the zoo<strong>, we saw every kangaroo</strong> jump <em><sup> and quite a </sup></em>few carried <a href="#"> babies</a>.</p>
        <button type="button" class="showplaintext">Get Plain Text</button>
        <button type="button" class="showformattedtext">Get Formatted Text</button>

    Taken from @ Get the text inside an element using jQuery

    0 讨论(0)
  • 2020-11-28 11:46

    You can get div content using .text() in jquery

    var divContent = $('#field-function_purpose').text();
    console.log(divContent);
    

    Fiddle

    0 讨论(0)
  • 2020-11-28 11:52

    Use .text() to extract the content of the div

    var text = $('#field-function_purpose').text()
    
    0 讨论(0)
提交回复
热议问题