I have a JavaScript function code where I want to alert.

function msgalert(x,y)
{
 tempstr = x.value
if(tempstr.length>y)
 {
  alert(c_AcknowledgementText);
  x.value = tempstr.substring(0,y);
  }
}

Now I have an xml with below format:

<?xml version="1.0" encoding="utf-8" ?>
<root>
    <key name="c_ContactUsHeading">Contact Us</key>
    <key name="c_AcknowledgementText">Comments can not be more than 250 characters.</key>
</root>

I want the JavaScript code so that the message shown in above alert can be read from above xml key name "c_AcknowledgementText".

I am trying with the below code, but unable to solve the problem, can you please have a look

    <script language="javascript" type="text/javascript">
function msgalert(x,y)
{
tempstr = x.value
    if(tempstr.length>y)
     {
            $(document).ready(function(){
        		$.ajax({
        			type: "GET",
        			url: "../includes/ResourceData.xml",
        			dataType: "xml",
        			success: function(xml) {					
        				$(xml).find('key').each(function(){
        					var title = $(this).find('name').text();						
        					);
        				});					
        			}
        		});
        	});
}
}
        </script>

some where I need to modify the above function so that I can use it to give alert value through XML.

share|improve this question

2 Answers

up vote 2 down vote accepted

I can't really understand what you're trying to do, but this code is wrong:

var title = $(this).find('name').text();

At that point, this is the current <key> element you are looping through. To get the value of the name attribute of this element, you would need to do this:

var title = $(this).attr('name');

And then to get the contents of this element, you would do:

var title = $(this).text();

In response to your comment, I think you want this:

var title = $(this).find('key[name=c_AcknowledgementText]').text();
alert(title);
share|improve this answer
I want to match that if attribute name="c_AcknowledgementText" then it should show the alert message of the text given in attribute c_AcknowledgementText. How can I do it? – Manoj Singh Jun 26 '09 at 11:50

So there are some different ways to go about this problem, you could

$(xml).find([name='key']).each(function(){
    alert($(this).text());                                                
});

This finds all elements where the name attribute equals 'key', and then it posts an alert with the text of that element.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.