0

I have a global var named inputType which is a lengthy object:

inputType = {
part1: 'string',
part2: 'string2',
...
part100:'last string'
}

In a separate piece of code, I have some input fields that receive numeric values. I would like to alter certain parts of the var object string as such:

<input id='input1'>First input

code:

$(document).ready(function(){

$('#input1').blur(function (){
 var inp = parseInt($('#input1').val(), 10);

 if (inp <10) {
 inputType.part2.replace(/string2/,'some other string'); 
}

});
});

// Then I call this modified  inputType.part2 object in another function

// I can't seem to effect the string change when the condition is met? When I try to call this particular object property in another script, the string change is not performed. I also can't see this change on the DOM inspector when I try this out. But this syntax will perform the replace function when I try it on the console.

How can I replace or modify inputType.part2 when the condition is met, and then use the modified object in another piece of code?

Objects are tough!

Thanks.

user1837608
  • 930
  • 2
  • 11
  • 37
  • [Please read the documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace). `.replace` **returns** a new string. (also `var` is an invalid variable name, but I assume you have a proper name in your code). – Felix Kling Dec 21 '13 at 17:28
  • possible duplicate of [Replace method doesn't work](http://stackoverflow.com/questions/1433212/replace-method-doesnt-work) – Felix Kling Dec 21 '13 at 17:29
  • -1 for posting such messy code. indent it properly. – goat Dec 21 '13 at 17:40

2 Answers2

1

Name your object first. Then try to access it with that name.

var xObj = {
part1: 'string',
part2: 'string2',
...
part100:'last string'
}

.....

if (inp <10) {
  xObj.part2.replace(/string2/,'some other string'); 
}

Edit:

.replace() will not modify the source string. we have to assign the result of .replace() back to the source to accomplish the modification.

Conceptual DEMO

Your code should be like this,

if (inp <10) {
 xObj.part2 = xObj.part2.replace(/string2/,'some other string'); 
}

Please read here to know more about .replace()

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
  • I assume that the variable has a proper name in the actual code, otherwise the OP would get a syntax error and the code wouldn't work at all. You haven't fixed the real problem. – Felix Kling Dec 21 '13 at 17:31
  • @FelixKling I was confused about it in the beginning, But see OP just mentioned in all places like `var.part2`. Ok let us clarify this with him. :) – Rajaprabhu Aravindasamy Dec 21 '13 at 17:34
  • Sorry about the confusion. My variable is global with a unique name, I have corrected this in the question. I don't think this is the issue, since I can still reference this particular object property in its unmodified state (even if the condition is met). – user1837608 Dec 21 '13 at 17:35
  • @user1837608 You have to assign it back to the object property to get your concept working. – Rajaprabhu Aravindasamy Dec 21 '13 at 17:37
  • Thanks @RajaprabhuAravindasamy for the tip. the missing element was indeed the lack of reassigning the new string back to the object. This is a great help to me! I appreciate it. – user1837608 Dec 21 '13 at 19:16
0

Try this:

window.InputVal = {
   part1: 'string',
   part2: 'string2',
   ...
   part100:'last string'
}

.....

if (inp <10) {
  window.InputVal.part2 = window.InputVal.part2.replace(/string2/,'some other string'); 
}

just 'update' the window.InputVal.part2 with the new value. Put only the .replace will not override the old value.

For example:

// define "x"
window.InputVal.x = 1;

// replace value
window.InputVal.x.toString().replace(/1/,'2');

// show
console.log(window.InputVal.x)
// will print 1

// but..if you do:

window.InputVal.x = window.InputVal.x.toString().replace(/1/,'2');
// will print '2' 
Eduardo Stuart
  • 2,869
  • 18
  • 22