Click here to Skip to main content
15,911,360 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
I tried input(width,height) and div.But iam not getting how to resize the div which contain image using input provided.As u change the input ,div should  change,at the same time image also

C#
1.	Write a JavaScript function which accepts two inputs namely the height and width of a div and change it according to the input provided. The div should contain an image which will also resize based on the size of the div. If the div tag reaches the maximum size of the image then it should not increase in size and throw an alert saying "Maximum resize limit reached".


What I have tried:

HTML
<html>
<head>
<style>
#size {
position:absolute;
height:500;
width:500;
top:20%;
left:30%;
margin:-100px 0 0 -150px;
}
</style>
<script language="text/javascript">
function myFunction()
{
document.getElementById ("size").style.height;
document.getElementById ("size").style.width;
</script>
</head>
<body>
height:<input type="number" value="height"><br>
width:<input type="number" value="width" ><br>
<div id="size" align="center" style="border:1px solid red">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTwnuyTru-iUDbPmRb-uHqzTfg3YDHkj_BNfReDUmb5awGMGbaa" style="width:304px;height:228px;">
</div>
<input type="button" value="resize" onclick="myFunction()"><br>
</body>
</html>
Posted
Updated 6-May-16 0:50am

You've already been told that no-one is going to do your homework for you, please don't repost the question, consult your tutors if you are struggling, or at least break the problem down into sections and do some basic googling.
 
Share this answer
 
Comments
Member 12504833 6-May-16 7:02am    
Iam not saying to do my homework....I Want u to show my mistake that's it.
1. Provide Id to your controls:
HTML
<input type="number" value="height" id="txtHeight">
<input type="number" value="width" id="txtWidth"></input></input>

2. Remove the space preceding braces. Change:
JavaScript
document.getElementById ("size")

to
JavaScript
document.getElementById("size")

The correct code becomes:
HTML
<html>
<head>
<style>
#size {
position:absolute;
height:500;
width:500;
top:20%;
left:30%;
margin:-100px 0 0 -150px;
}
</style>
<script>
function myFunction()
{
document.getElementById("size").style.height = document.getElementById("txtHeight").value + "px";
document.getElementById("size").style.width = document.getElementById("txtWidth").value + "px";
}
</script>
</head>
<body>
height:<input type="number" value="height" id="txtHeight"><br>
width:<input type="number" value="width" id="txtWidth"><br>
<div id="size" align="center" style="border:1px solid red">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTwnuyTru-iUDbPmRb-uHqzTfg3YDHkj_BNfReDUmb5awGMGbaa" style="width:304px;height:228px;">
</img></div>
<input type="button" value="resize" onclick="myFunction();"><br>
</body>
</html></br></br></br>


But I would like to suggest:
1. Check for validation(valid values for height and width)
2. Use JQuery for cross- browser support. It's always good.

Hope it helps.
 
Share this answer
 
Comments
Member 12504833 6-May-16 6:56am    
Thank u soooooooooooooo much zafar Sultan.....Thank u very much...I want this only.
Zafar Sultan 6-May-16 7:00am    
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900