-2

I got this code from the web but it doesn't seem to work.. Is the syntax all right? It is from http://verlager.com/super-dev.php which contains names that should be capitalized. The problem is that the strings are all uppcase, so I have to change them to lowercase and then capitalize the first and last name.

JS:

if ($temp > "") { $temp.addClass('capitalize'); // what should I use?
var $full = $temp.split(","); var $nick = $full[0]; $name = $nick.substr( 0, 16);
$("#I" + i).val($name + ", " + $full[1].substr(0, 1) + ". " + members.find(x => x.Name === $temp).Rating);
}

CSS:

.capitalize {text-transform:capitalize;} //$temp is all caps
verlager
  • 794
  • 5
  • 25
  • 43
  • 1
    Please provide a [mcve] that reproduces the problem you are having. If `$temp` is a jQuery object it will not pass a *"greater than"* statement comparing it to a string – charlietfl May 26 '18 at 02:04
  • I can't post the url to the web because this is a prototype development version. We changed the data source and I have to maintain the old version on the web. – verlager May 26 '18 at 02:07
  • That's not how things work here. Post the basic code here if you want help – charlietfl May 26 '18 at 02:08
  • Still, what is $temp? You must have some code that sets it. – Bill Doughty May 26 '18 at 02:08
  • 2
    Possible duplicate of [With jQuery, how do I capitalize the first letter of a text field while the user is still editing that field?](https://stackoverflow.com/questions/2017456/with-jquery-how-do-i-capitalize-the-first-letter-of-a-text-field-while-the-user) – wp78de May 26 '18 at 02:08
  • $temp is a name as in "last, first" – verlager May 26 '18 at 02:08
  • OK I will risk it. One sec. – verlager May 26 '18 at 02:09
  • Then you should be seeing error in browser console because there is no `addClass` prototype for strings – charlietfl May 26 '18 at 02:09
  • This question is **not** a duplicate. I have already lowercased the $temp string in css file. I'd like to add a class now to capitalize the $temp string. – verlager May 26 '18 at 02:27

1 Answers1

1

The syntax is not alright. I'm going to assume that temp is a string. Your class will work but must target the correct DOM element instead of operating on the string for DOM actions like addClass. Below change someDOMelement to be the element selector that represents the string to have the capitalization class added. Using browser dev tools make sure that the class rules aren't being overridden somehow. If you need to update the value of the element use text()

if ($temp && $temp.length > 0) { 
    $(<someDOMelement>).addClass("capitalize");
    // ... rest of your string formatting and value setting
}
Kelson Olson
  • 142
  • 2
  • 10
  • temp is already lowercase string. See http://verlager.com/super-dev.php I just want to capitalize each word in the string "temp". – verlager May 26 '18 at 03:15
  • if ($temp && $temp.length > 0) { $("temp").addClass("capitalise"); ===> doesn't seem to work. – verlager May 26 '18 at 04:02
  • Of course it doesn't work. Did you even read my post? I think I just found a troll on stack overflow lol. Go learn how JQuery works and then you can do your homework. – Kelson Olson May 26 '18 at 20:14