Microsoft Interview Question
Software Engineer / DevelopersTeam: Global Foundation Services
Country: United States
Interview Type: In-Person
<div id="paragraph">
...
</div>
/******************************/
jQuery part:
$("div#paragraph").click(function(){
alert("something") //alert something
});
Hope this will help
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());
});
});
<p>paragraph</p>
<script>
$(document).ready(function() {
$('p').click(function() {
console.log('Click event fired');
if($('p').text() == 'paragraph'){
alert("hello");}
});
});
</script>
<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>
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());
});
});
This would work:
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:
Then your code could look like:
- Anonymous September 29, 2014