Avatar of impressionexpress
impressionexpress
 asked on

Sort through html in a string

The following is the code I use to find what Im looking for in actual HTML
		var dataArray = new Array();
		$('.info_byrequests').each(function(){
			var appId = $(this).attr('data-appId');
			if(jQuery.inArray(appId, dataArray) == -1){
				dataArray.push(appId);
			}
		});	

Open in new window


However, now I need to do the same thing with HTML in a string
        get_applications().done(function(value) {
			var html_string = value;
                        // sort and compare results with actual HTML
			$('#list_app').html(html_string);
        });	

Open in new window

HTMLJavaScriptjQuery

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Julian Hansen

you can pass HTML directly to jQuery and then query it
var html = '<div><p id="abc">We want this one</p><p>We don\'t want this one</p></div>';
var jq = $(html);
var found = jq.find('#abc');
console.log(found);

Open in new window

In your case it is not clear what you want to do with the string - if you could explain a bit more I can better advise you.

The above code demonstrates how to use an HTML string in the same way you would an HTML document using jQuery.
impressionexpress

ASKER
This is my html string I get from an ajax call
echo "<div data-appId=\"" . $result[ 'application_id' ] . "\" class=\"form_title info_byrequests date\">" . str_replace( " ", "-", $result[ 'app_date' ] ) . "</div>";

Open in new window


I need to search for every instance of data-appId=(Number compared by in javascript array) since there will be many and remove the intire div before displaying html to screen
			for(var i=0; i<=dataArray.length; i++){
			//	console.log('ID: ' + dataArray[i]);

			}

Open in new window

Julian Hansen

Still not clear on what you are asking.
I need to search for every instance of data-appId
This is straight forward - find all <div> that has a custom data attribute appId
Number compared by in javascript array
Not sure what you mean here
remove the intire div before displaying html to screen
Which entire div? Based on what criteria.

Can you fill in the gaps as what you have explained does not give the full picture.

In the meantime consider this example

var html = '<div data-appId="123" class="form_title info_byrequests date">20200101</div><div></div><div></div>';
var jq = $(html);
jq.each(function(index, value) {
  if (value.dataset.appid) {
   // this element has a data-appId value
  }
})

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
impressionexpress

ASKER
The entire html string in PHP
		echo "<div data-appId=\"" . $result[ 'application_id' ] . "\" class=\"form_title info_byrequests date\">" . str_replace( " ", "-", $result[ 'app_date' ] ) . "</div>" . chr(13);
		echo "<div data-appId=\"" . $result[ 'application_id' ] . "\" class=\"form_title info_byrequests appNumber\">" . str_replace( " ", "", $result[ 'app_date' ] ) . "-" . $result[ 'application_id' ] . "</div>";
		echo "<div data-appId=\"" . $result[ 'application_id' ] . "\" class=\"form_title info_byrequests name\">" . strtoupper($result[ 'last_name' ] . ", " . $result[ 'first_name' ]) . "</div>";
		echo "<div data-appId=\"" . $result[ 'application_id' ] . "\" class=\"form_title info_byrequests phone\"><span class=\"telephone\">" . $result[ 'telephone' ] . "</span></div>";
		echo "<div data-appId=\"" . $result[ 'application_id' ] . "\" class=\"form_title info_byrequests creditType\">" . $result[ 'credit_type' ] . "</div>";
		echo "<div data-appId=\"" . $result[ 'application_id' ] . "\" class=\"form_title info_byrequests creditAmount\"><span class=\"dollar_sign\">$</span><span class=\"requested_credit\">" . $result[ 'credit_amount' ] . "</span></div>";
		echo "<div data-appId=\"" . $result[ 'application_id' ] . "\" class=\"form_title info_byrequests status\"><i class=\"" . $result[ 'class' ] . "\" title=\"" . $result[ 'status_name' ] . "\"></i></div>";

		echo "<div class=\"clear_all\"></div>";

Open in new window

lets say I have a javascript array as follows
var dataArray = new Array("55", "56", "57");

Open in new window

As you can see there are many div's with data-appId that will have the same value. the data value is compared with data already displayed to screen. to avoid duplicates, I want to remove all the div's that have data-appId=55 & 56 & 57 (as seen in the array) before I append the html string to the current html
Julian Hansen

Where does the array come from?
Do you need to progressively update it  - or is it fixed with those numbers?

My last post shows how to iterate over the content - you can easily add your code to do the array check. I can provide code but I need to understand exactly what the landscape is.

As you can see there are many div's with data-appId that will have the same value.
I can't see - there is no data that shows this.

You have not explained where the Array comes from - nor have you explained what must happen.

for instance - if the data received from the AJAX call contains the appId's 10,11,12,13 and you already have 10 and 11 showing - you only want to show 12 and 13? So we check each incoming appId against the array - if we find one that does not match - we then add that to the screen - AND then update the array with this id?

If you could clarify that bit for me please.
impressionexpress

ASKER
This is where the array comes from, The following code is inside function populate_applications()
                // determine what data we already have
		var dataArray = new Array();
		$('.info_byrequests').each(function(){
			var appId = $(this).attr('data-appId');
			if(jQuery.inArray(appId, dataArray) == -1){
				dataArray.push(appId);
			}
		});	

	setInterval(function() {
		populate_applications();
	//	console.log('Update');
	}, 5000);

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

You did not answer my other questions?

This is taking much longer than it should - if you could just read my posts and answer my questions we can wrap this up.

So far I have given you everything you need to solve this problem - but if you want a complete solution I need to understand the problem space properly.
impressionexpress

ASKER
Which questions are you looking for me to answer ?
Julian Hansen

nor have you explained what must happen.

for instance - if the data received from the AJAX call contains the appId's 10,11,12,13 and you already have 10 and 11 showing - you only want to show 12 and 13? So we check each incoming appId against the array - if we find one that does not match - we then add that to the screen - AND then update the array with this id?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
impressionexpress

ASKER
Yes, Julian. that's exactly what needs to happen.

what I tried before was clearing the div and repopulating which causes a sort of flash on update. this is annoying. Im looking for a smooth solution.
Julian Hansen

This has gone a bit stale so I need to get on top of what the issue is.

You are getting some HTML by AJAX.
You need to parse the HTML and extract items from the HTML and add them to the page but only if they are unique.

Is the page rendered with items OR area all items progressively added with AJAX calls - need to know if we must pre-populate the array on page load or if we can start with [] and then add to it with each AJAX?
impressionexpress

ASKER
Im getting HTML by AJAX
I need to add the HTML to the page only if its unique

When you land on the page at first, it will display all the proper results however, if there are records added to the database while on that page, it should append that added record
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

When you land on the page at first, it will display all the proper results
How do the id's from those results get into the array?
impressionexpress

ASKER
I create the array and add all the id's
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.