2

I need to change the text as this is outputting as y2015 while it should only be 2015 and the thing this will be within a set of select option and they could be dynamic as per y2012 y2013 y2014

So basically this

<select>
  <option>y2013</option>
  <option>y2014</option>
  <option>y2015</option>
</select>

Should become

<select>
  <option>2013</option>
  <option>2014</option>
  <option>2015</option>
</select>

I have found a question called How to see if string contains substring which may help but I cannot figure out how to use it for my case.

It basically says to use if (str.indexOf("Yes") >= 0) which could work as i could say something like if (str.indexOf("Y2") >= 0) and then use subtring(1);

The y+number is needed for my web address, I am changing the address for the history and if it uses numbers only it gets confused. I have found out that by adding a letter before the number that would fix the address issue therefore I cannot output numbers only as per 2015 etc.

Also notice that this is not my only dropdown and I cannot provide a specific class just for this one, so i cannot target it directly otherwise it would have been much easier.

Community
  • 1
  • 1
rob.m
  • 9,843
  • 19
  • 73
  • 162
  • 1
    Why not fix whatever is outputting the `y` rather than trying to replace it later on ? – adeneo Jan 26 '15 at 16:50
  • because the y+number is need for my web address, I am changing the adreess for the history and if it uses numbers it gets confused. i have found out that by adding a letter before the number that would fix the issue. - I will add this bit of info in my question – rob.m Jan 26 '15 at 16:51
  • So why not add `y+number` as a data attribute which can be read separately from the human-facing text value? Using javascript to hack around basic UI is not a very good idea IMO. – Rory McCrossan Jan 26 '15 at 16:57
  • i totally agree about the hacking, but it's just a whole complication, the data attribute cannot be added because the whole address bar is built using big lines and to change the way they work, like using data attribute for the address history value, will become a nightmare. Also is a whole mixup of php and jQuery so not that easy peasy. Nothing wrong with a simple naughty hack ;) – rob.m Jan 26 '15 at 17:02

1 Answers1

3

Simply call substr(1) on the option element's text(), checking if 'y' is the first character:

$('option').text(function() {
  var text = $(this).text();
  return text.indexOf('y') == 0 ? text.substring(1) : text;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select>
  <option>y2013</option>
  <option>y2014</option>
  <option>y2015</option>
</select>

<select>
  <option>2013</option>
  <option>2014</option>
  <option>2015</option>
</select>
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
  • ok yes that would have been easy but I have a set of various option selects int he page and I have no class. I am trying to check it by finding the text which is that and do the substring.. – rob.m Jan 26 '15 at 16:52