PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 27, 2022

[FIXED] How handle $.getJSON file with strings and integer?

 October 27, 2022     getjson, javascript, jquery, json     No comments   

Issue

how can I parse Integer from the JSON file? Or what is the problem below?

$('#search').keyup(function(){

        var searchField = $('#search').val();

        var myExp = new RegExp(searchField, "i");

     $.getJSON('data.json', function(data){
            if(searchField === '')  {
                $('#update').html('');
                return;}
            var output ='<ul class="searchresults">';

            $.each(data, function(key, val){

            if(val.number.search(myExp) != -1) {
                output += '<li>';
                output += '<p>' + val.number + '</p>';
                output += '<p>' + val.name + '</p>';
                output += '</li>';
            }
            });

            output += '</ul>';
            $('#update').html(output);

        });

    });

What is missing?

Error is:

Uncaught TypeError: val.number.search is not a function

Many thanks in advance!

Br


Solution

Probably it's a number, numbers don't have a search method. You could use .toString().search(...) to convert it to a string first.



Answered By - CherryDT
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing