Solved

JavaScript/jQuery: Get attribute value from string

Posted on 2013-05-23
1
407 Views
Last Modified: 2013-06-03
Using jQuery or pure JavaScript, from this code, how can I get "Hello World"?

<script type="text/javascript">

var data = '<' + '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"' + '><' + 'html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"' + '><' + 'head' + '><' + 'title>Title</title' + '><' + 'meta name="description" content="HR Home Description" /' + '><' + 'script type="text/javascript"' + '><' + '/script' + '><' + '/head' + '><' + 'body class="Hello World"><p>Invalid HTML HERE</div' + '><' + '/html>';

// I want the alert to say: "Hello World"
alert(data);

</script>

Open in new window

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39192478
var msg = data.split("body class=")[1].split(">")[0].replace(/["']/g, "");
alert(msg);

Open in new window

0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

736 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question