atomapps
atomapps

Reputation: 193

text() jQuery Not permanently changing values

I'm not sure exactly if this is a caching or coding issue. Interestingly when I try to change the value of a div using text() it works sometimes and does not work other times...

EDIT SOLVED SEE BELOW: The issue ended up being some foul part of the code re-submitting the page a second time, thus giving the impression that the variables were not being stored, or the html()/text() was not working correctly...

<body onLoad="PrintLabel()">

And right before the closing /body:

$v = "<?php echo $inputbarcode; ?>";
    function PrintLabel() {
    if( $v === "") {
    }
        else {
            $indexarray = '<?php echo $final; ?>';
                if($indexarray !== "" ){
                $('#invalid').text('Added to Stocktake.');
                }
                else {
                $('#invalid').text('Barcode not found!');
                alert("Barcode not found!");
                }
        }
}

The div as written within the body:

<div id="invalid">Please scan a barcode to begin.</div>

Upvotes: 0

Views: 136

Answers (4)

atomapps
atomapps

Reputation: 193

The issue ended up being some foul part of the code re-submitting the page a second time, thus giving the impression that the variables were not being stored, or the html()/text() was not working correctly...

This is the code which has now been removed:

$('#userInput').keyup(function(e){
 if(e.keyCode == 13)
  {
   $(this).trigger("enterKey");    
  }
});

Thank you to all who contributed and helped solve the issue.

Upvotes: 0

Jeric Cruz
Jeric Cruz

Reputation: 1909

change .text to .html

function changeVal(){
  $("#sample").html("New Value");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="sample">Sample</div>

<button id="change" onclick="changeVal();">Change div text</button>

Upvotes: 1

Sumit patel
Sumit patel

Reputation: 3903

Print Text into div in two method

Method-1 using text()

$('#invalid').text('Please scan a barcode to begin.');

Example

$('#gettext').click(function()
{
$('#invalid').text('Please scan a barcode to begin.');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="invalid"></div>
<input type="button" id="gettext" value="get text"/>

Method-2 using html()

$('#invalid').text('Please scan a barcode to begin.');

Example

$('#gettext').click(function()
{
$('#invalid').html('Please scan a barcode to begin.');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="invalid"></div>
<input type="button" id="gettext" value="get text"/>

Upvotes: 0

Jon Walker
Jon Walker

Reputation: 143

You are mixing PHP and Javascript. I believe what you are going for is something like this which implements jQuery's $.html() method which is what you want on a div

var v = "<?php echo $inputbarcode; ?>";

function PrintLabel() {

if( v == "" ) {

    // do nothing

} else {

    var i = "<?php echo $final; ?>";

      if(i !== "" ) {

          $("#invalid").html("Added to Stocktake.");

      } else {

            $("#invalid").html("Barcode not found!");

            alert("Barcode not found!");

      }

   }

}

Upvotes: 0

Related Questions