0

I am forming the div dynamically on document load like below. I have a ajax call and on response fro the AJAXcall I want to replace the value 'right' with the value from the AJAX call using jQuery.

Basically I want to replace the value for the text div mentioned below once AJAX has the success data.

var beginContainer = '<div class="container">';
var endDiv = '</div>';
var beginInner = '<div class="inner">';
var text = '<div class="text" id=3 style="color:#0000FF" align ="center">';
var append = beginContainer + beginInner + text + 'Number of sensors' + endDiv + text + '10cents/sensor' + endDiv + endDiv + beginInner + text +'right'+endDiv +endDiv;

$('#content').append(append);  

var beginContainer = '<div class="container">';
var endDiv = '</div>';
var beginInner = '<div class="inner">';
var text = '<div class="text" id=4 style="color:#0000FF" align ="center" value="kkk">';
var append = beginContainer + beginInner + text + 'Total Bill' + endDiv + text + 'Month-to-Month' + endDiv + endDiv + beginInner + text +'right'+endDiv +endDiv;

$('#content').append(append);
5
  • 1
    Where's the AJAX call? Commented May 5, 2015 at 22:41
  • Anyway... The easiest way is to wrap the text on a span and give this element an ID. So you can refer to it by its ID later. Commented May 5, 2015 at 22:42
  • 1
    FYI, you have id=3 in text variable's div, and in the append variable, you are using text multiple times. This will create multiple elements with the same id, which is not proper HTML and is considered a very bad practice. I think you should consider fixing this and perhaps refactor the code to make it easier to spot before working on the AJAX or you'll be in for a lot more headache down the line. Commented May 5, 2015 at 22:50
  • I can get rid off the id tag on text. I am not using it any way. Commented May 5, 2015 at 22:54
  • 2
    The div element doesn't have an attribute value. Also, the use of the attribute align is deprecated. Commented May 5, 2015 at 22:57

2 Answers 2

3

You could use a span tag, something like:

var append = beginContainer + beginInner 
    + text + 'Number of sensors' + endDiv + text 
    + '10cents/sensor' + endDiv + endDiv + beginInner 
    + text + '<span id="yourSpan">right</span>' 
    + endDiv + endDiv;

So you can use reference it by:

$("#yourSpan").html("ajaxResult");
Sign up to request clarification or add additional context in comments.

Comments

-2

use this for your ajax call:

$.ajax("URL.com",function(e){
  $("#wherever").html(e);
});

3 Comments

This will replace the whole content of the selected element.
he said "i want to replace the value for the text div mentioned below once ajax has the success data", so if you run: $(".text").html(e);
The OP wants to replace a specific word that's part of the element's content, not its entire content. Read the question again. I want replace the value 'right' with the value from the AJAX call

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.