user1048676
user1048676

Reputation: 10066

Get Value of Div after append function

All, I'm using the following code:

$(document).on('change','.found_other_vendor',function(){
if(checked){
    alert("it was checked");
    if($("#other_liked_vendors").val()!=""){
        alert("second_one");
        $.post(ajaxurl, { clicked_id: clicked_id, action: action }, function(results){
            $("#other_liked_vendors").append(", " + results);
        });
        $("#other_liked_vendors")
    }else{
        alert("first_one");
        $.post(ajaxurl, { clicked_id: clicked_id, action: action }, function(results){
            $("#other_liked_vendors").html(results);
        });
    }
}
}

For some reason it always says that my first if statement for:

if($("#other_liked_vendors").val()!="")

Is always coming up false even after I've put data in my div with the html. Any idea why it isn't recognize a value in my div even after I put data in it?

Thanks!

Upvotes: 0

Views: 1525

Answers (3)

Sam Rueby
Sam Rueby

Reputation: 6129

It doesn't seem you're using val() correctly. Documentation for val():

The .val() method is primarily used to get the values of form elements such as input, select and textarea. In the case of elements, the .val() method returns an array containing each selected option; if no option is selected, it returns null.

Upvotes: 1

amit_g
amit_g

Reputation: 31270

Did you mean to write

$("#other_liked_vendors").html()

as it seems like "#other_liked_vendors" is a div. You can use val() only with form elements. You have to use html() or text() for elements like div.

Upvotes: 1

SLaks
SLaks

Reputation: 887987

.val() returns the value of a form element.
You want .text() or .html().

Upvotes: 1

Related Questions