0

I am writing a function which is used to replace the class of div in certain conditions, for example,

  1. condition A - the div class is e_new_hazard_frame_2_1_4_7, I want to replace the 2 to 1, outcome should be like this - e_new_hazard_frame_1_1_4_7
  2. condition B - the div class is the same with above, I want to replace the 1 to 4, outcome should be like this - e_new_hazard_frame_2_4_4_7
  3. condition C - the div class is the same with above, I want to replace the 4 to 6, outcome should be like this - e_new_hazard_frame_2_1_6_7
  4. condition D - the div class is the same with above, I want to replace the 7 to 3, outcome should be like this - e_new_hazard_frame_2_1_4_3

these 4 numbers might be the same, like 1_1_1_1, my demo coding is like var str = 'e_new_hazard_frame_2_1'; str = str.replace(/_[_0-9]*)$/,'1');

it seems not correct can anyone teach me how to do it? thank you so much!

5 Answers 5

1

Try this one.It may help you.

<div class="e_new_hazard_frame_2_1_4_7">click to change value</div>
<script type="text/javascript">
    $(".e_new_hazard_frame_2_1_4_7").on('click',function(){
    change_class(7); //change the value from here i.e 2,4,1,7
});

function change_class(p)
{
    var number_change=p;
    switch (number_change)
    {
        case 2:
        var str='e_new_hazard_frame_2_1_4_7';  
        str = str.replace(/[2]/g,'1');
        break;
        case 1:
        var str='e_new_hazard_frame_2_1_4_7';  
        str = str.replace(/[1]/g,'4');
        break;
        case 4:
        var str='e_new_hazard_frame_2_1_4_7';  
        str = str.replace(/[4]/g,'6');
        break;
        case 7:
        var str='e_new_hazard_frame_2_1_4_7';  
        str = str.replace(/[7]/g,'3');
        break;    
    }
$(".e_new_hazard_frame_2_1_4_7").html(str);
}
</script>

Please let me know if you face any problem.

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe use a function to handle the numbers however you want?

replace Javascript | MDN

function replacer(match, p1, p2, p3, p4, offset, string){
  // p1-4 are your numbers 
  // (they are strings right now, sto make +p1 to convert them to int)
  p1= +p1 + 1;
  p2= +p2 + 1;
  p3= +p3 + 1;
  p4= +p4 + 1;

  // fixed a small bug here
  return ['', p1, p2, p3, p4].join('_');
};

var str = "asdasdasd_1_2_3_4";
str = str.replace(/_(\d)_(\d)_(\d)_(\d)/, replacer);

1 Comment

just fixed a small bug I had. Should work now. NOTE: it only works when all numbers are single digits.
0

This will replace all occurrence of 2 to 1 in str:

var str = 'e_new_hazard_frame_2_1_2_4'; 
str = str.replace(/2/g,'1'); 

To replace the class of your div:

var target_class = 'e_new_hazard_frame_2_1_4_7';
$('.'+target_class).removeClass(target_class).addClass(target_class.replace(/2/g,'1'))

Comments

0

In PHP

    echo str_replace("2","1","e_new_hazard_frame_2_1_4_7");

For more reference visit this. Hope this will help you.

In JavaScript

 str.replace("e_new_hazard_frame_2_1_2_4","e_new_hazard_frame_1_1_2_4");

For more reference visit this

1 Comment

He want to do it with JavaScript.
0

I think it might be useful to you `str='e_new_hazard_frame_2_1';

if(conditin1) 
{
 pattern = /[2]/g;
 str = str.replace(pattern,'1');
}

if(conditin2)
{
pattern = /[1]/g;
str = str.replace(pattern,'4');
}

if(conditin3)
{ 
 pattern = /[4]/g;
 str = str.replace(pattern,'6');
}

if(conditin4)
{
 pattern = /[7]/g;
 str = str.replace(pattern,'3');
}`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.