-5

I have following string in javascript.

var str = 'P24 + P33'; //p24 is just exp. it will be any number i.e. P98

I Want to replace this string into following string using jquery replace.

var str = "$('#p24').val() + $('#p33').val()";
Mohan Kute
  • 137
  • 1
  • 3
  • 14

1 Answers1

-1

var str = 'P24 + P33'; //p24 is just exp. it will be any number i.e. P98
var str_array = str.split(" + ");
console.log("Original string: "+str);
for(var i = 0; i < str_array.length; i++){
  str_array[i] = $("#"+str_array[i]).html();
}
var replaced_string = str_array.join(" + ");
console.log("Replaced string: "+replaced_string);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="P24">this is P24</div>
<div id="P33">this is P33</div>
Dave
  • 2,764
  • 2
  • 15
  • 27