0

Unable to get the correct value from the variable in side the function.

Here is my code and always i am getting id as "two", i need the correct value that i passed to change function.

<script type="text/javascript">
<?php
   $val='';
   global $val;
?>


function change(val){
  <?php //global $val;?>
      if (val=='one') {
         <?php $val='one';?>
      }
      if (val=='two') {
           <?php $val='two';?>
      }
}
</script>
<input type="button" onclick="change('one');" value="Change">

<div id="<?php echo $val;?>">
3
  • You should post the rendered html code. Such a mix of php/javascript is almost never a good idea. Commented Apr 2, 2014 at 8:58
  • It is looks like you try to mix using of client & server side code. This is really bad idea regardless of your goals. Commented Apr 2, 2014 at 8:59
  • Many apps are a mix of client & server code. But they are written by engineers who know what is happening on the server and client. Commented Apr 2, 2014 at 9:00

2 Answers 2

1

Well the PHP code executed independent of your javascript code, which is only executed afterwards on the client side. So what your script does is executing this in PHP:

<?php
global $val;
$val='one';
$val='two';
?>
<!-- ... -->
<div id="<?php echo $val;?>">

and this is the code on the client side:

<script type="text/javascript">
    function change(val){

      if (val=='one') {
      }
      if (val=='two') {
      }
     }
</script>
<input type="button" onclick="change('one');" value="Change">

So if you want to change an element you'll have to do it in Javascript:

<script type="text/javascript">    
    function change(val){
      var el = document.getElementsByTagName('div')[0];

      if (val=='one') {
        el.id = 'one';
      }
      if (val=='two') {
        el.id = 'two';
      }
     }

</script>
<input type="button" onclick="change('one');" value="Change">

<div id="">
Sign up to request clarification or add additional context in comments.

3 Comments

Is "" a valid HTML id? Would it be better to make it one or the other, and then use document.getElementById ?
I've kept my changes small and left the id uninitialised, because it is in the original question. But yes it's invalid. And yes I'd also have a unique selector for the changed element (maybe a class). I also don't see how changing the id could be useful ...
Thanks a lot, it helps and working.
1

You are mixing server-side-script and client-side-script try it in javascript like,

SCRIPT

<script type="text/javascript">
    var val='one'; // global value
    function change(){
        if (val=='one') {
           // your code for one
           val='two'; // change the value of val to two
        }
        else if (val=='two') {
            // your code for two
            val='one'; // again change val value
        }
        document.getElementsByClassName('changed_value')[0].id=val;
        document.getElementById(val).innerHTML=val;
    }
</script>

HTML

<input type="button" onclick="change();" value="Change">
<div class="changed_value"></div>

Live Demo

3 Comments

Thanks for the quick reply, i need to print the "val" value in the html code like <div id="{val}"></div>
Try my updated answer and demo
I have done my task with the above suggestion's thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.