Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove first char of a variable with jQuery

I'm trying to modify a jQuery variable from e.g. /images/tree.jpg to images/tree.jpg (without the first slash, so it should be a relative path instead of an absolute).

I get the URL like this: var href = jQuery("img.thumbnail").attr("href");

Now I need this URL on another place, but without the first char.

Is there an easy way to do this? Thanks!!

like image 204
m4mpfi Avatar asked Jul 25 '11 14:07

m4mpfi


People also ask

How do I remove the first character in jQuery?

You can also remove the first character from a string using substring method. let input = "codehandbook" function removeCharacter(str){ return str. substring(1) } let output = removeCharacter(input); console. log(`Output is ${output}`);

How to remove character in jQuery?

text(). replace('-', '') );

How do I remove the first word from a string in jQuery?

To remove the first word from a string, call the indexOf() method to get the index of the first space in the string. Then use the substring() method to get a portion of the string, with the first word removed.


2 Answers

var href = jQuery('img.thumbnail').attr('href');  // e.g.  '#foo'
var secondCharacterAndOnward = href.substring(1); // e.g.  'foo'

String.substring

Though I'm not sure why an image tag has a href attribute, to be quite honest...

like image 101
Brad Christie Avatar answered Oct 15 '22 12:10

Brad Christie


Use the javascript substring method.

var href = jQuery("img.Thumbnail").attr("href");
    href = href.substring(1);
like image 34
ShankarSangoli Avatar answered Oct 15 '22 11:10

ShankarSangoli