Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Client-side Scripting _ replace text in anchor text

Posted by: Tiff 1998 May 12 2017, 06:48 PM

I have a list of movies that when the link has focus sends the link text to an input feild that I use for a search function.
I would like to do the same for tv shows.

My files are currently formatted as

CODE
<a href="showname s01e01.mp4">Showname s01e01</a>


The text I need to send to the input field is
Showname&Season=01&Episode=01

Is this possible without changing the link text that is displayed on the screen?

This is the script that I use to send the text to the input field.
CODE

$(function () {
    $('#rightbox a').on('focus', function () {
        var text = $('#moviename');
        text.val($(this).text());
var fileName =$(this).text();
        fileName = fileName.replace(/\.[^/.]+$/, "");
        text.val(fileName);    
        $Form.click();
    });
});


I'm guessing I need some regex in here, but I'm not good enough at regex yet.

Posted by: Christian J May 13 2017, 08:08 AM

QUOTE(Tiff 1998 @ May 13 2017, 01:48 AM) *

My files are currently formatted as
CODE
<a href="showname s01e01.mp4">Showname s01e01</a>


As a sidenote, it's best not to use spaces in file names.

QUOTE
The text I need to send to the input field is
Showname&Season=01&Episode=01

That looks like a URL querystring. Are the INPUT field values used to send database queries? In that case maybe there are better ways to create the whole application.

QUOTE
Is this possible without changing the link text that is displayed on the screen?

Maybe, but it's probably not simpler than changing the link text. You might use javascript split() and replace() and do something like:

1. Split "Showname s01e01" by the space, so you get "Showname" and "s01e01". This will fail if "Showname" itself contains spaces, though.
2. Split "s01e01" by the "e", so you get "s01" and "01".
4. In "s01", replace the "s" with "&Season=".
5. In "01", prepend "&Episode=".
6. Add it all together again after "Showname".

Posted by: Tiff 1998 May 16 2017, 06:53 AM

Sorry Christian I got carried away and forgot to come back. I got it sorted out with this:

CODE

$("#rightbox a").focus(function() {
  var title = $(this).text(),
    isShow = (/s(\d+)e(\d+)/i).test(title),
    rgx, rplcment;

   rgx = (isShow) ? / s(\d+)e(\d+).*/i : /(\w+(\.\w+)*)\.\w+/;
  rplcmnt = (isShow) ? '&Season=$1&Episode=$2' : '$1';

  title = title.replace(rgx, rplcmnt);
  $('#moviename').val(title);
$Form.click();
});



Thank you for your time though smile.gif

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)