1

What I need to do:

  • I need to match Json with string.

case 1: When industry data is set in Json

        ["", "app.php", "automotive", "tradeshows"]

JQuery code:

  $( document ).ready(function() 
 {


    var match =  window.location.pathname.split("/");
    console.log(match[3]);

    if(match[3]=="conferences")
    {
         console.log("true");
    $('#radio3').prop('checked', true);

    }
    else if(match[3]=="tradeshows")
    {
        console.log("afeef");
        $('#radio2').prop('checked', true);
    }
    else
    {
        $('#radio1').prop('checked', true);
    }



   });

case 2: When industry data is not set in Json

     ["", "app.php",  "tradeshows"]
  • In this case match would be failed.

  • Is there any other way in match Json with string in JQuery?

  • I need to match the Json with string so is there any other way so that I could match Json with string?

2
  • Looks like you just want to match JSON attribute value against the string value and not the entire JSON object, am I right? Commented Jul 10, 2015 at 9:20
  • You could use: var match = window.location.pathname.split("/"); if(match.length < 4) {$('#radio1').prop('checked', true); return;} //rest of code.... But FYI, this has nothing to do with JSON but javascript array, JSON is an object notation Commented Jul 10, 2015 at 9:21

1 Answer 1

1

You can use jQuery indexOf() to detect if automotive is available or not. See the below code

function myFunction(page) {
    var pages = ["", "app.php", "automotive", "tradeshows"];
    var a = pages.indexOf(page);
    document.getElementById("demo").innerHTML = a;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Click the button to display the position of the element "tradeshows":</p>

<button onclick="myFunction('automotive')">Check for automotive</button>
<button onclick="myFunction('automotive1')">Check for automotive1</button>

<p id="demo"></p>

Note: The indexOf method was introduced in JavaScript 1.6, and does not work in Internet Explorer 8 and earlier versions.

Sign up to request clarification or add additional context in comments.

1 Comment

How is it relevant to the question?

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.