I have a wordpress site that im list out files in a directory. The code gets spit out as
<a href="http://helpdesk-3/acme/wp-content/uploads/important_documents/compliance_documents/sample_document.pdf">sample_document.pdf</a>
Im trying to wrap my head around using jQuery to re-write the link display text to camel case, no extension, and spaces. So it would run and re-write it as
<a href="http://helpdesk-3/acme/wp-content/uploads/important_documents/compliance_documents/sample_document.pdf">Sample Document</a>
Does anyone have a suggestion?
DEMO: http://jsfiddle.net/f6x9P/1/
Since you’re using WordPress, it would probably be easier to just use PHP.
This will take care of spaces.
When you pass
.text()
a function, you are asking to loop over the elements selected, and update their text. Whatever youreturn
will be the new text, and the anonymous function is passed the index of the current element in the selection and the current element’s value: http://api.jquery.com/textVery likely need to create a far better selector than looping every a tag in page
Sure. But strainght javascript is used for string manipulation. You want to get the text value of the anchor:
atext = $('a').text();
Then apparently you want to strip off the pdf extension. I’ll assume there is only one period in the filename, and extension could be anything:
atext = atext.split('.');
atext[0] now contains everything to left of period. Assume that underscore is the seperator:
atext = atext[0].split('_');
camel = ''
finally change the text in the tag:
$('a').text(camel);
There are things you’ll need to improve here. Using jQuery to get ‘a’ will return all anchors; so change the match string to be more specific. Or, if you do want to change all anchor tags, then you need to put all the string manipulation code in a function and apply it to all the matched anchor tags.