0

I am trying to get array value from data tag to jQuery but not working. Here is my code:

//$b is taking names from text-area separated by comma.
$a = array_map('trim', explode(",", $b));
return <input type="text" id="pct"><button id="pcthit">submit</button><p id="pct_avail"></p></span>
<input type="hidden" id="storageElement" data-storeIt="'.$a.'">';


$("#pcthit").click(function(){
    var pstcde = $("#pct").val();
    var retrieved_string = [];
    var retrieved_string = $("#storageElement").data('storeit');
    if((pstcde != '') && (jQuery.inArray(pstcde, retrieved_string) >= 0 )){
    $("#pct_avail").html('hello');
    }
});

1 Answer 1

1

You need to encode the array with json_encode().

return '<input type="text" id="pct"><button id="pcthit">submit</button>
        <p id="pct_avail"></p></span>
        <input type="hidden" id="storageElement" data-storeIt=\''.json_encode($a).'\'>';
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Barmar, I tried with json_encode() but still not working. $("#pct_avail").html(retrieved_string); is giving only bracket "[".
i replaced all the quotes for input, ( " ) with ( ' ) and ( ' ) with ( " ), now it's working. Thanks a lot for your help :),
I just changed the quotes around the data-storeIt value, since json_encode() uses double quotes around all the embedded strings.
wow, it's a easy way rather than changing all the quotes, thanks :)

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.