Microsoft Interview Question for Software Engineer / Developers


Team: Global Foundation Services
Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
1
of 1 vote

This would work:

$('p').on('click', function() {
  alert( $(this).text() );
});

If you want the event to also bind to paragraphs that aren't present on page load (due to AJAX loading, for example) then you can apply jQuery's .on() method to a parent element. It's also helpful to namespace your event bindings, as it makes it easier to remove the event later if you wish. So given the markup:

<div id="wrapper">
  <p>Some text in the paragraph.</p>
  <p>And another paragraph, and so on...</p>
</div>

Then your code could look like:

$('#wrapper').on('click.alert', 'p', function() {
  alert( $(this).text() );
});

- Anonymous September 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

<div id="paragraph">
...
</div>
/******************************/
jQuery part:
$("div#paragraph").click(function(){
alert("something") //alert something
});

Hope this will help

- zhujy8833 January 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do you mean the alert box should show the content in the paragraph?

I think the answer should be:

<p>
Hello, world!
</p>
<p>
My name is QQQQ.
</p>


/* jquery*/
$("p").each(function(){
$(this).click(function(){
alert($(this).text());
});
});

- Anonymous January 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do you mean the alert box should show the content in the paragraph?

I think the answer should be:

<p>
Hello, world!
</p>
<p>
My name is QQQQ.
</p>


/* jquery*/
$("p").each(function(){
$(this).click(function(){
alert($(this).text());
});
});

- Anonymous January 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The above answer (using $.each and tag name instead of id) is great. Also add $(document).ready . And <pre> tags instead of <p> to preserve the formatting

- goku.post July 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<p>paragraph</p>

<script>
$(document).ready(function() {
 $('p').click(function() {
       console.log('Click event fired');
	if($('p').text() == 'paragraph'){
alert("hello");}

 
});
});
</script>

- janellec August 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, forgot to take out the log messaging.

<p>paragraph</p>


<script>
$(document).ready(function() {
$('p').click(function() {
if($('p').text() == 'paragraph'){
alert("hello");}
});
});
</script>

- janellec August 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<div class="para-content">
<p>Hello World</p>// paragraph content which show on click
</div>

<script type="text/javascript">
$(document).ready(function(){
$(".para-content").click(function(){
alert($(this).find("p").text());/* after clicking the paragraph show alert message with the paragraph content.;
});
});
</script>

- rupesh kumar April 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If the intention of the question is test only jQuery skills, you should attach a handler for the element/div for the click event.

<html>
  <title>Test Page</title>
  <head>
  </head>
  <body>
    <div id='elem'>
      Hello World!
    </div>
  </body>
</html>

jQuery:

$(document).ready(function () {
  $('#elem').bind('click', function() {
    alert($('#elem').html());
  });
});

- kunalc June 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

$(document).ready(function(){
  $('p.paraone').bind('click', function(){
  //alert('ready');
    alert($('p.paraone').text());
   
  });
});

- tkarthikeyan January 03, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More