Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery print value of variable

Tags:

jquery

I am new to jQuery and I need some assistance with very simple code. So, I have a string variable that I want to print in the p with listprice class.

<script> 

$(document).ready( function() {
var string = "US $257.31";
$('.listprice).append(string);
}
</script>

Any ideas how can I achieve that?

Thank you,

H

like image 689
bobek Avatar asked Dec 16 '22 11:12

bobek


2 Answers

You could do this

var string = "US $257.31";
$('.listprice').html(string);

http://jsfiddle.net/jasongennaro/akpn3/

Of course, this assumes there is nothing in the p as it would replace everything in there.

To add it on a separate line when the p contains content, do this

$('.listprice').append('<br />' + string);

http://jsfiddle.net/jasongennaro/akpn3/1/

EDIT

As per your comment

Do you know how can I do that with regular javascript? I think they are blocking jquery in there.

You could do this

var string = "US $257.31";

var a = document.getElementsByTagName('p');

a[0].innerHTML += string;

http://jsfiddle.net/jasongennaro/akpn3/2/

This finds the first p and adds the variable string.

like image 67
Jason Gennaro Avatar answered Jan 05 '23 18:01

Jason Gennaro


You're missing a '.

$('.listprice).append(string);

should be

$('.listprice').append(string);

EDIT: Also missing a ) at the end.

Other than that it's fine.

like image 43
James Montagne Avatar answered Jan 05 '23 18:01

James Montagne