0

I am trying to write a javascript code that would change the value of rect when a specific event occurs. I am thinking along the lines of document.getElementById("xxx").setAttribute.. but I am completely stuck. Any help would be greatly appreciated!

The html code I have is this.

var x = document.getElementById("some integer").value;

<div id="xxx" style="position: absolute; left: 500px; top: 520px; width: 220px; height: 10px; clip: rect(0pt, 10px, 10px, 0pt); background-color: rgb(0, 0, 255);">

I want to set the second value in rect, ie (10px) to xpx.

3
  • To what tag / attribute do you want to make it? Commented Jun 21, 2011 at 16:50
  • so for example it is currently rect(10px, 10px, 10px, 10px) and I want to change it to rect(10px, Zx, 10px, 10px) where z is an integer Commented Jun 21, 2011 at 16:55
  • I understand now. You want to change a CSS property of an element. That is different from an attribute. E.g. style or id are attributes. Commented Jun 21, 2011 at 16:57

3 Answers 3

1

document.getElementById("xxx").style.clip = "rect(10px, 10px, 10px, 10px)"; should do the trick.

The clip property is a CSS property, so you need to use the style property of the DOM element to set it.

Sign up to request clarification or add additional context in comments.

Comments

0

This is my one suggesion..

document.getElementById("xxx").setAttribute('onclick', function() {document.getElementById("xxx").style.clip="rect(0px 75px 75px 0px)"; });

or try this...

document.getElementById("xxx").style.clip="rect(0px 75px 75px 0px)";

3 Comments

what if I have z = 100 and I want to set the value to that. Do I do: document.getElementById("xxx").style.clip="rect(0px " +Z+"px" + "75px 0px)"?
what if I have z = 100 and I want to set the value to that. Do I do: document.getElementById("xxx").style.clip="rect(0px " +Z+"px" + "75px 0px)"?
@Paul: That is the same comment as before?
0

You could try something like this, so that you can change any/all of the clip settings:

function changeClipTo(t,r,b,l,elementID) {

var clipString = "rect(" + t + "px " + r + "px " + b + "px " + l + "px)";
document.getElementById(elementID).style.clip = clipString;

}

That way you can have any click event supply your desired values such as onclick="changeClipTo(0,50,10,0,'someElement');" or href="javascript:changeClipTo(0,50,10,0,'someElement');"

If you don't know the dimensions ahead of time, then you could use the onclick/href to call a function which determines the value you need, then call changeClipTo() from that function, passing in the computed value(s).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.