Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript jQuery find substring

System32

Active Coder
So, I have this code:

JavaScript:
$(document).ready(function() {
            $(".share_post").click(function(event) {
                var post = $(this).parent().parent().parent().parent().html();            
                alert(post); // Shows html
                event.preventDefault();
            });
        });
The HTML I get is:

Code:
<div id="3" class="post-header"><h2><i class="material-icons">description</i><img src="https://2img.net/i/empty.gif" alt="[solved]nbeka nova tema Empty" loading="lazy"><span class="post-heading-wrap"><a href="/t2-solvednbeka-nova-tema#3">[solved]nbeka nova tema</a><span class="post-date">Wed 16 Nov 2022 - 10:40</span></span></h2><div class="mobile-hide post-buttons"><ul><li class="btn-quote-multi"><span title="Select/Unselect multi-quote" id="post_mq2_3" onclick="togglePostMultiQuote(this);"><i class="material-icons">format_quote</i><i class="material-icons">add</i></span></li><li class="btn-quote"><a href="/post?p=3&amp;mode=quote" title="Reply with quote"><i class="material-icons">format_quote</i></a></li><li class="btn-edit"><a href="/post?p=3&amp;mode=editpost" title="Edit/Delete this post"><i class="material-icons">edit</i></a></li><li class="btn-delete"><a href="/post?p=3&amp;tid=43df79a271e9e3ff7a4a3af2f9b1a8d1&amp;mode=delete" title="Delete this post"><i class="material-icons">delete</i></a></li><li class="btn-ip"><a href="/modcp?mode=ip&amp;p=3&amp;t=2&amp;tid=43df79a271e9e3ff7a4a3af2f9b1a8d1" title="View IP address of poster"><i class="material-icons">info</i></a></li><li class="share_post"><i class="material-icons">share</i></li>                              </ul></div><div class="mobile-show dropdown post-buttons-mobile"><i class="material-icons">more_horiz</i><ul class="dropdown-box"><li class="btn-quote-multi"><span title="Select/Unselect multi-quote" id="post_mq2_3" onclick="togglePostMultiQuote(this);"><i class="material-icons">format_quote</i><i class="material-icons">add</i></span></li><li class="btn-quote"><a href="/post?p=3&amp;mode=quote" title="Reply with quote"><i class="material-icons">format_quote</i></a></li><li class="btn-edit"><a href="/post?p=3&amp;mode=editpost" title="Edit/Delete this post"><i class="material-icons">edit</i></a></li><li class="btn-delete"><a href="/post?p=3&amp;tid=43df79a271e9e3ff7a4a3af2f9b1a8d1&amp;mode=delete" title="Delete this post"><i class="material-icons">delete</i></a></li><li class="btn-ip"><a href="/modcp?mode=ip&amp;p=3&amp;t=2&amp;tid=43df79a271e9e3ff7a4a3af2f9b1a8d1" title="View IP address of poster"><i class="material-icons">info</i></a></li></ul></div></div><div class="post-body"><div class="post"><div class="post-content">                                  <div class="personal-vote">                                     <a href="" class="vote-up"><i class="material-icons">keyboard_arrow_up</i></a><h4>0</h4><a href="" class="vote-down"><i class="material-icons">keyboard_arrow_downp</i></a>                                 </div><!--
 REPUTATION SYSTEM  --><div>random tekst<br><br>dfgdfg</div><br><br>Last edited by Admin on Mon 28 Nov 2022 - 20:25; edited 2 times in total (Reason for editing : test)</div><div class="post-footer likes-active vote-active"><div class="fa_like_div"><button class="rep-button fa_liked" data-href="/vote_like?cvf%5B0%5D=358013163b670b431c6e06e945524d63&amp;cvf%5B1%5D=e4da8661604a937ccab27790ccf2baf3&amp;l_vote=3&amp;like_val=3" data-href-rm="/vote_like?cvf%5B0%5D=d563efcabf02b6c7f382e68685a634ac&amp;cvf%5B1%5D=fefd1e2b3bbcafafd96e82becb3a4a6d&amp;l_vote=3&amp;like_val=5"><span class="rep-cat"><i class="material-icons">thumb_up</i><span class="rep-nb">2</span></span><span>Like</span></button><button class="rep-button fa_dislike" data-href="/vote_like?cvf%5B0%5D=fd9223540c0ace38e28fbf8f4968cedc&amp;cvf%5B1%5D=81a5dad70fd4bc4d5026c514884f2a1c&amp;l_vote=3&amp;like_val=4" data-href-rm="/vote_like?cvf%5B0%5D=d563efcabf02b6c7f382e68685a634ac&amp;cvf%5B1%5D=fefd1e2b3bbcafafd96e82becb3a4a6d&amp;l_vote=3&amp;like_val=5"><span class="rep-cat"><i class="material-icons">thumb_down</i><span class="rep-nb" style="display:none;"></span></span><span>Sad</span></button><p class="fa_like_list"><a href="/u1">Admin</a> and <a href="/u2">testacc</a> like this post</p><p class="fa_dislike_list" style="display: none;"></p></div><div class="vote"><div class="vote-bar" title="Message reputation : 100% (2 votes)"><div class="vote-bar-desc">Message reputation : 100% (2 votes)</div><div class="vote-bars"><div class="vote-bar-plus" style="width:50px;"></div></div></div></div></div></div><aside class="post-aside"><div class="post-author-status"></div><div class="post-author"><span class="post-author-name"><a href="/u1"><span style="color:#000099"><strong>Admin</strong></span></a></span><span class="post-author-title">Admin<br></span></div><div class="post-author-avatar"><div class="avatar-big"><a href="/u1"><img loading="lazy" src="https://2img.net/i/fa/modernbb/pp-blank-thumb.png" alt="avatar"></a></div></div><div class="dd_award right box-body" style="display: none;"></div><div class="award_more box-body"></div><br><dl class="post-author-details"><dt><span class="label"><span style="color:#263238;"><img src="https://i.ibb.co/StLYbWS/7665584-25x25.png" alt="&nbsp;&nbsp;Title"></span></span></dt><dd>Dreamer</dd><dt><span class="label"><span style="color:#263238;">Posts</span> : </span></dt><dd>14</dd><dt><span class="label"><span style="color:#263238;">Join date</span> : </span></dt><dd>2022-11-12</dd></dl><div class="post-author-rpg"></div><div class="post-author-contact dropdown"><i class="material-icons">keyboard_arrow_down</i><div class="dropdown-box"><a href="/u1" class="profile-icon" title="View user profile"><img src="https://2img.net/i/fa/awesomebb/icon_user_profile.png" class="i_icon_profile   " alt="View user profile" loading="lazy"></a><a href="/privmsg?mode=post&amp;u=1" class="profile-icon" title="Send private message"><img src="https://2img.net/i/fa/awesomebb/icon_contact_pm.png" class="i_icon_pm   " alt="Send private message" loading="lazy"></a><a href="/profile?mode=email&amp;u=1"><img src="https://2img.net/i/fa/awesomebb/icon_contact_email.png" alt="Send e-mail" title="Send e-mail" loading="lazy"></a><a href="https://testaca.forumotion.me" title="Visit poster's website" target="_blank" rel="nofollow"><img src="https://2img.net/i/fa/awesomebb/icon_contact_www.png" alt="https://testaca.forumotion.me"></a></div></div></aside></div>


All I need is to extract first link href and ignore everything else. And first href value is: /t2-solvednbeka-nova-tema#3

How can I do that? :/
 
Solution
I managed to do it in worst possible, yet working way :Rofl:

JavaScript:
var post = $(this).parent().parent().parent().parent().html().toString(); // Get full HTML of parent
        var start = post.indexOf("<a href"); // Get first occurrence of link
        var uneditedLink = post.substring(start, start + 50); // Get 50 more chars after start of the link
        var startLink = uneditedLink.indexOf("/"); // Get index of / position before taking post URL
        var endLink = uneditedLink.lastIndexOf('"'); // Get index of last occurrence of double quote

        var postLink = uneditedLink.substring(startLink, endLink); // Get post link
I managed to do it in worst possible, yet working way :Rofl:

JavaScript:
var post = $(this).parent().parent().parent().parent().html().toString(); // Get full HTML of parent
        var start = post.indexOf("<a href"); // Get first occurrence of link
        var uneditedLink = post.substring(start, start + 50); // Get 50 more chars after start of the link
        var startLink = uneditedLink.indexOf("/"); // Get index of / position before taking post URL
        var endLink = uneditedLink.lastIndexOf('"'); // Get index of last occurrence of double quote

        var postLink = uneditedLink.substring(startLink, endLink); // Get post link
 
Solution
I managed to do it in worst possible, yet working way :Rofl:

JavaScript:
var post = $(this).parent().parent().parent().parent().html().toString(); // Get full HTML of parent
        var start = post.indexOf("<a href"); // Get first occurrence of link
        var uneditedLink = post.substring(start, start + 50); // Get 50 more chars after start of the link
        var startLink = uneditedLink.indexOf("/"); // Get index of / position before taking post URL
        var endLink = uneditedLink.lastIndexOf('"'); // Get index of last occurrence of double quote

        var postLink = uneditedLink.substring(startLink, endLink); // Get post link
XD lol unfortunately, that is the nature of the beast. Sometimes, the hackiest, inefficient code possible will be the only way to go...I know the feeling oh too well XD
 
XD lol unfortunately, that is the nature of the beast. Sometimes, the hackiest, inefficient code possible will be the only way to go...I know the feeling oh too well XD
I was a guy who wanted somebody experienced to solve this but then I was like "Hmmm, nobody will waste their time for free so it's time to waste one day for this stupid thing" :Rofl:
Can you discuss with admin to add post count under rank? I kinda feel odd when there is no such thing. Thanks. :D
 
It's good you fixed it yourself, because the HTML you posted in two extremely long lines is all but unreadable 😐 Why do such a thing ?
I'm not too keen about the assumptions made by this script, specifically the parent hierarchy and the max length (arbitrary 50) of a link. Murphy dictates that one day this will break e.g. if the HTML gets updated with a extra div or span or such. But ok, if you're happy with this hack, go with it !
 
It's good you fixed it yourself, because the HTML you posted in two extremely long lines is all but unreadable 😐 Why do such a thing ?
I'm not too keen about the assumptions made by this script, specifically the parent hierarchy and the max length (arbitrary 50) of a link. Murphy dictates that one day this will break e.g. if the HTML gets updated with a extra div or span or such. But ok, if you're happy with this hack, go with it !
It's not like I am making this for a comany where they earn money. Forumotion forums are for people who wish to build some community (small one). And they do not change original html once made. If they change something, they update it as a new version. But update should be like a real change, not one div change. :x3:

They got like phpBB2 version which is old as hell and then they made AwesomeBB that contains materialize design introduced. A huge change in html templates.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom