2

I'm not good at JQuery at all, in fact this is my first encounter due to Shopify. In other words I'm completely lost.

This is what I was able to do so far.

function FindPlayer()
{

    var playerid = $('input[id=playerId]').val();
    var clubname = $('input[id=teamname]').val();


    $("#searchBar").attr('data-user-input', value);
    $.ajax({
        url: "http://website.com/index.php?player=" + playerid + "&club=" + clubname,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            response(data);
        }
    });

}

The json response is going to look like this:

  [{"playerFound":"true","tradeid":"123456"}]

I want to then check if playerFound is true or false before setting this element:

  <input id="tradeId" type="hidden" name="attributes[tradeid]" />

This is probably pretty basic for JQuery users but not for me any help would be appericiated.

2 Answers 2

1

Try This:-

$.ajax({
    url: "http://website.com/index.php?player=" + playerid + "&club=" + clubname,
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        if(data[0].playerFound == "true")
        {
           $('#tradeId').val(data[0].tradeid);
        }
    }
 });
Sign up to request clarification or add additional context in comments.

1 Comment

JQuery is such a confusing language but from what I can see it's very efficient.
0

Since content type is JSON you can simply use like this

   success: function (data) {               
            if(data.playerFound == "true"){
                $('#tradeId').attr('name',data.tradeid) // if you want to change name
                $('#tradeId').val(data.tradeid)// if you want to change value
            }          
   }

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.