Home > Enterprise >  Javascript targeting specific element and removing characters from it
Javascript targeting specific element and removing characters from it

Time:04-15

I have a really complicated looking path that I want to target first of all I don't what to use to get the path so javascript can target it, I have

  1. Selector
  2. Js Path
  3. Xpath
  4. Full Xpath

Then I want to remove a text from it, namely the word 'by'

Here's the code I frankenstiened together from all the things said by many other users:

$('/html/body/div[1]/div[3]/div/div[2]/div/div[1]/div/div/div[1]/section/div[2]/div[1]/div[2]/div[2]/div/div');
{
$(this).html( $(this).html().replace("by",""); 
});

If you don't understand the code I tried to make, I first of all:

  1. tried to target a path with Xpath and put it as a variable
  2. I used this.html to target it (no idea why i used 2 but that's what one of the answers from this site)
  3. Used .replace to attempt to replace the words "by Mishaal; Masked Man"

This did not work and gave me the error: "Uncaught SyntaxError: missing ) after argument list"

image

Help appreciated, thank you.

CodePudding user response:

The example is not very clearly explained, I also don't know what specifically other users advised, the specific case is missing but from guesses I can deduce that you are using jquery. Try to pull xpath to a variable but not as a jquery object $(...) but as a regular string and only use .replace() on it.

const path = '/html/body/div[1]/div[3]/div/div[2]/div/div[1]/div/div/div[1]/section/div[2]/div[1]/div[2]/div[2]/div/div';

const formatted = path.replace('be', '');

CodePudding user response:

Thank you very much everyone, I frankensteined everything I saw on webpages and concluded with this:

var element = document.evaluate( '/html/body/div[1]/div[3]/div/div[2]/div/div[1]/div/div/div[1]/section/div[2]/div[1]/div[2]/div[2]/div/div' ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;

  element.textContent = element.textContent.replace('by ','')

it looks messy but it works.

  • Related