Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery change <p> text programmatically

EDIT: The solution was to add this to the profile page instead of the gender page.

$('#profile').live( 'pageinit',function(event){ $('p#pTest').text(localStorage.getItem('gender')); 

});

I have a paragraph with som text in a listview that I want to change programatically from another page after clikcing save.

EDIT: This is my listview in profile.html. When you click on the item you get to another page where you can save your gender. I want to change the paragraph in this listview to the gender that was changed from the other page.

<ul data-role="listview"   >     <li><a href="gender.html">         <img src="images/gender2.jpg" />         <h3>Gender</h3>         <p id="pTest">Male</p>     </a></li> </ul> 

The gender html page is just basic stuff with two radio buttons and a save button. Here is my javascript code(in a seperate file):

$('#gender').live('pageinit', function(event) {      var gender = localStorage.getItem('gender');     var boolMale = true;     if (gender == "female") boolMale = false;     $('#radio-choice-male').attr("checked",boolMale).checkboxradio("refresh");     $('#radio-choice-female').attr("checked",!boolMale).checkboxradio("refresh");      $('#saveGenderButton').click(function() {         if ($('#radio-choice-male').is(':checked'))             localStorage.setItem('gender', "male");         else localStorage.setItem('gender', "female");          $('#pTest').html('test'); //does not work          //$('p#pTest').text('test'); //does not work          //$('#pTest').text('test'); //does not work         $.mobile.changePage("profile.html");     }); }); 

I have tried this with javascript: $('p#pTest').text('test');

The text does not change however. (I know that the save button works). Is this possible?

like image 511
oivindth Avatar asked May 07 '12 17:05

oivindth


People also ask

Which jQuery function is used to prevent code from running before the document is finished loading?

The Document Ready Event This is to prevent any jQuery code from running before the document is finished loading (is ready). It is good practice to wait for the document to be fully loaded and ready before working with it.

What is the use of this keyword in jQuery?

The this Keyword is a reference to DOM elements of invocation. We can call all DOM methods on it. $() is a jQuery constructor and in $(this), we are just passing this as a parameter so that we can use the jQuery function and methods.

What is jQuery and how do you use it?

jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website. jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.


1 Answers

Try the following, note that when the user refreshes the page, the value is "Male" again, data should be stored on a database.

$('button').click(function(){      $('#pTest').text('test') })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>  <p id="pTest">Male</p> <button>change</button>
like image 83
undefined Avatar answered Oct 02 '22 18:10

undefined