0

I'm having one page, choosemerchant where I get the list of merchants. After selecting a list item I want to append that list item value to the textbox id which belongs to a different html page. Here is my code:

$('.ui-li-icon li').click(function() {
    var index = $(this).index();
     text = $(this).text();
    alert('Index is: ' + index + ' and text is ' + text);
    sessionStorage.setItem("selectedMerchant", text);
    window.location="recommendmanual.html";
    $('#merchant').append(text);
});

And in my page which contains textbox id=merchant I have put this code in script:

var selectedMerchant = "";
if ( sessionStorage.getItem("selectedMerchant") ) {
    selectedMerchant = sessionStorage.getItem("selectedMerchant");
}

It worked before, but now it's not working.

7
  • Comment out code until it works. Since it worked before, the error must be with something you added since then. If you have a version control system installed (which you should), you can compare the old and the new code easily. Commented Apr 23, 2014 at 12:22
  • Try to describe in detail in what way it's not working. Do you get a javascript error in the console? Commented Apr 23, 2014 at 12:23
  • you say #merchant is in next page, but how you append in click of first page ? doesn't this comes in next set of code you mentioned ? Commented Apr 23, 2014 at 12:42
  • @David my code is working upto alert('Index is: ' + index + ' and text is ' + text); after this its going to that recommendation page also.and in divsion.its showing empty data. Commented Apr 23, 2014 at 13:16
  • 1
    @user3415421:Ok, visit this page videmadesign.herobo.com/choosemerchant.html, when you click on li you'll navigate to recommendmanual.html and you'll see the merchant in the text box. You can use firebug to see the code that is the same of my answer. Commented Apr 24, 2014 at 7:52

3 Answers 3

1

I don't know if I understand correctly your question and if this will help you
But if you want by clicking on an li element in your choosemerchant page the clicked item text appears inside the texarea on the page recommendmanual
this is the code for choosemerchant page:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('.ui-li-icon li').click(function() {
        var index = $(this).index();
        var text = $(this).text();
        sessionStorage.setItem("selectedMerchant", text);
        console.log(sessionStorage)
        window.location="recommendmanual.html";
    });     
})
</script>
<ul class="ui-li-icon">
  <li>Merchant 1</li>
  <li>Merchant 2</li>
  <li>Merchant 3</li>
  <li>Merchant 4</li>
</ul>

and this is the code for your recommendmanual page

<script type="text/javascript">
$(document).ready(function(){
    var what=sessionStorage['selectedMerchant']
    $('textarea').val(what)
})
</script>
<textarea name="merchant" cols="100" rows="10"></textarea>

With this solution if you click on one li item in the first page text will appear in the second page textarea.
if i have not understand correctly your question and this solution does not suit your needs, i apologize for making you lose time.

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

1 Comment

w3schools reference on HTML5 web storage: w3schools.com/html/html5_webstorage.asp
1

the second of these two lines:

 ...
 window.location="recommendmanual.html";
 $('#merchant').append(text);
 ....

will not be reached: the browser will immediately start loading "recommendmanual.html" and stop executing the current script.

3 Comments

yes..so how can i change the value of that merchant id to the value of merchant name..?
@user3415421 The answer by Devima above solves this issue by storing the data to append using sessionStorage. I believe you can make that work. You could also use HTTP GET. window.location="recommendmanual.html?append="+text; and then check the value of window.location on the page recommendmanual.html
even if you swap these lines, this has no visible effect, since the browser will start loading the new page and the user will not see the altered value anyway.
-1

one thing append text to #merchant before you redirect to other page i.e., $('#merchant').append(text); this should be before window.location="recommendmanual.html";

1 Comment

Won't work. The first line will modify the DOM on the current page, the second line will load a new DOM, discarding the changes made by the first line.

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.